# Get-ADUser errors out weirdly

**URL:** <https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150>\
**Category:** PowerShell Help\
**Created:** [April 7, 2016, 12:17am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150 "2016-04-07T00:17:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![windows-liveuser72](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@windows-liveuser72](https://forums.powershell.org/u/windows-liveuser72)\
**Post date:** [April 7, 2016, 12:17am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/1 "2016-04-07T00:17:56Z")

</div>

```
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…

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 7, 2016, 12:34am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/2 "2016-04-07T00:34:24Z")

</div>

Did you tried Googling that? 😉 😛 hehehe…

Anyway, try this:

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

---

<div class="post-metadata">

**Author:** ![windows-liveuser72](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@windows-liveuser72](https://forums.powershell.org/u/windows-liveuser72)\
**Post date:** [April 7, 2016, 12:46am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/3 "2016-04-07T00:46:19Z")

</div>

🙂  
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

---

<div class="post-metadata">

**Author:** ![windows-liveuser72](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@windows-liveuser72](https://forums.powershell.org/u/windows-liveuser72)\
**Post date:** [April 7, 2016, 12:50am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/4 "2016-04-07T00:50:10Z")

</div>

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

Thanks!

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 7, 2016, 12:52am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/5 "2016-04-07T00:52:43Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![windows-liveuser72](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@windows-liveuser72](https://forums.powershell.org/u/windows-liveuser72)\
**Post date:** [April 7, 2016, 12:53am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/6 "2016-04-07T00:53:21Z")

</div>

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

```
'$a.txt'
$a.txt
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 7, 2016, 12:55am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/7 "2016-04-07T00:55:42Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![windows-liveuser72](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@windows-liveuser72](https://forums.powershell.org/u/windows-liveuser72)\
**Post date:** [April 7, 2016, 1:08am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/8 "2016-04-07T01:08:39Z")

</div>

omg. yeah, 3 hours totally wasted.

Thanks folks! case closed!

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 7, 2016, 1:09am UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/9 "2016-04-07T01:09:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/get-aduser-errors-out-weirdly/6150/10 "2024-05-16T20:41:36Z")

</div>


