# using get-aduser command in Powershell

**URL:** <https://forums.powershell.org/t/using-get-aduser-command-in-powershell/11373>\
**Category:** PowerShell Help\
**Created:** [September 25, 2018, 9:59am UTC](https://forums.powershell.org/t/using-get-aduser-command-in-powershell/11373 "2018-09-25T09:59:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicktargmail-com](https://avatars.discourse-cdn.com/v4/letter/n/8c91f0/32.png) [@nicktargmail-com](https://forums.powershell.org/u/nicktargmail-com)\
**Post date:** [September 25, 2018, 9:59am UTC](https://forums.powershell.org/t/using-get-aduser-command-in-powershell/11373/1 "2018-09-25T09:59:07Z")

</div>

Hi there,

I need some help on how to pass a powershell variable to get-aduser account. For example,

I can run this successfully:

get-aduser -filter {name -like “_smith_”} | select SamAccountName

but if I am reading a bunch of names for a file, then I need to be able to pass the names as a variable, like this as an example:

$username=“smith”

get-aduser -filter {name -like “_$username_”} | select SamAccountName

doesn’t work, I think the variable doesn’t get resolved. Any ideas how to fix it. Thank you.

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![alexw](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/alexw/32/74_2.png) [@alexw](https://forums.powershell.org/u/alexw)\
**Post date:** [September 25, 2018, 10:00am UTC](https://forums.powershell.org/t/using-get-aduser-command-in-powershell/11373/2 "2018-09-25T10:00:29Z")

</div>

There are different ways to write a filter for AD  
Try like so:

```
$username="smith"
get-aduser -filter "name -like '*$username*'" | select SamAccountName
```

---

<div class="post-metadata">

**Author:** ![nicktargmail-com](https://avatars.discourse-cdn.com/v4/letter/n/8c91f0/32.png) [@nicktargmail-com](https://forums.powershell.org/u/nicktargmail-com)\
**Post date:** [September 25, 2018, 10:10am UTC](https://forums.powershell.org/t/using-get-aduser-command-in-powershell/11373/3 "2018-09-25T10:10:02Z")

</div>

Awesome, thank you Alex,

I tried:

‘name -like “_$username_”’

but not

“name -like ‘_$username_’”

I thought single comma in front of a variable causes it not to resolve, but I guess I was wrong.

Thanks again

&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [September 25, 2018, 2:17pm UTC](https://forums.powershell.org/t/using-get-aduser-command-in-powershell/11373/4 "2018-09-25T14:17:42Z")

</div>

You are correct as well as wrong.

Single quote will not resolve variables, but double quotes do, that means if the single quotes are inside double quotes, it will resolve.

See below on quoting rules, I suggest you to read this document.

> **[about Quoting Rules - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.2&viewFallbackFrom=powershell-5.0)**
>
> Describes rules for using single and double quotation marks in PowerShell.

---

<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:34pm UTC](https://forums.powershell.org/t/using-get-aduser-command-in-powershell/11373/5 "2024-05-16T20:34:19Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


