Helping Others at Microsoft TechEd with PowerShell 911

While at Microsoft TechEd last week, I met a gentleman from Europe who was experiencing a particular issue with the Get-ADUser PowerShell cmdlet.

When Get-ADUser is used with a hard coded value such as name as shown in the following example, it returns the expected information without issue:

1Get-ADUser -Filter {Name -eq 'Administrator'}


The issue is that when the name, for example, is stored in a variable and double quotes are used to try to expand the variable, nothing is returned:

1$User = 'Administrator'
2Get-ADUser -Filter {Name -eq "$user"}


For the life of us, we couldn't lay our hands on a test system with the Active Directory cmdlets on it at TechEd and neither of us had a computer with us that particular day. Luckily I had a test PowerShell Web Access system exposed to the Internet that I was able to log into from my phone over 3G, replicate the problem, and come up with a solution:


I brought this up at the PowerShell dinner at TechEd and ended up speaking to the person who is in charge of the team that came up with PowerShell Web Access. He actually had me bring it up on my phone at the dinner and took a picture of me holding my phone up while logged into PSWA to take back to his team. A bit of trivia that he told me about was that PowerShell Web Access was originally called "PowerShell 911".

The solution I gave was to use double quotes instead of curly braces and single quotes around the variable since the quote marks on the outside always win:

1Get-ADUser -Filter "Name -eq '$user'"


I saw the same gentleman at the closing party and mentioned that dropping the quotes altogether would probably allow it to work inside the curly braces, and indeed it does:

1Get-ADUser -Filter {Name -eq $user}