Cannot use variable with Get-User -Filter in Exchange Management Console

console, exchange-server, powershell

Solution

I don't have access to this cmdlet, are you sure it takes a scriptblock and not a string? If it takes a string try this:

Get-User -Filter "SamAccountName -eq $SamAccountName"

If it really takes a scriptblock try:

Get-User -Filter {SamAccountName -eq $SamAccountName}.GetNewClosure()

Problem

I cannot seem to use variable in the situation below. ``` [PS] C:\>Get-User -Filter {SamAccountName -eq "Test.Smith"} Name RecipientType ---- ------------- Test Smith UserMailbox [PS] C:\>$SamAccountName = "Test.Smith" [PS] C:\>Get-User -Filter {SamAccountName -eq $SamAccountName} [PS] C:\>echo $SamAccountName Test.Smith [PS] C:\> ``` You can see the command works fine when I type out the name, but not when I use a variable. Thanks!

Original source