Get-ADUser errors out weirdly

Get-ADUser -Filter {samaccountname -eq $_.Samaccountname}
Get-ADUser : Error parsing query: 'samaccountname -eq $_.Samaccountname' Error Message:
'syntax error' at position: '20'.

I tried various things with filter but it will only return errors like this or returns null. Any ideas? I can’t seem to figure it out. I tried tracing and using ISE to debug, but I can’t understand why it won’t return anything…

Did you tried Googling that? :wink: :stuck_out_tongue: hehehe…

Anyway, try this:

Get-ADUser -Filter “SamAccountName -eq ‘$($_.SamAccountName)’”

:slight_smile:
Yeah, I’ve tried, and I’m still trying and its been 3 hours already that’s why I posted this really stupid question… but to the point.

Get-ADUser -Filter "samaccountname -eq $($a.txt)"
Get-ADUser : Error parsing query: 'samaccountname -eq palaginrv' Error Message: 'syntax error' at position: '20'.

What is that supposed to mean? O_o

Oh wait, I forgot ’ ’
but I thought that anything inside ’ ’ gets treated like text not expressions?

Thanks!

Put single quotes around $($a.txt) like this:

Get-ADUser -Filter "samaccountname -eq '$($a.txt)'"

Yeah, thanks! But why does it work that way? where do I read about that? :wink:

'$a.txt'
$a.txt

That whole thing is inside a double-quoted string:

"'$($a.txt)'"

So in this case, the single-quotes are just characters in the string; they have no special meaning to PowerShell.

However, on the inside of the AD functions, it’s taking that string an evaluating it as code. So the string you want to produce is:

SamAccountName -eq ‘SomeValue’

Long story short, the AD commands are weird and annoying. :slight_smile:

omg. yeah, 3 hours totally wasted.

Thanks folks! case closed!

The filter option is a bit wacky. The filter uses PowerShell Expression Language. If you look at the help, you see that the filter parameter takes in a string:

PS C:\> Get-Help Get-ADUser -Parameter Filter

-Filter 

    Required?                    true
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           Filter
    Aliases                      None
    Dynamic?                     true

So anything you will pass to the filter will be taken in as a string. To get the subexpression to work, so expanding the variable and return it as a string to the filter, you put these within single quotes.