Author Posts

September 1, 2015 at 7:02 am

Trying to Get-Aduser based on description. I have a csv with a list of serial numbers. These are added to the description field of Ad users. When I run my code nothing returns.

$csv = Import-Csv C:\scripting\chromebook1.csv 

foreach ($line in $csv)
{get-aduser -Filter {description -like $line} -SearchBase 'OU=LEADERSHIP,DC=DS,DC=NET'}

September 1, 2015 at 7:12 am

Two things here: One, the -Like operator requires some wildcards, if you're planning to look for a string that contains something else. Also, you need to be looking at $line.Description, rather than just $line. Try this:

$csv = Import-Csv C:\scripting\chromebook1.csv 

foreach ($line in $csv)
    get-aduser -Filter "description -like '*$($line.Description)*'" -SearchBase 'OU=LEADERSHIP,DC=DS,DC=NET'

I've changed your -Filter argument from a script block literal to a double-quoted string, based on past experience with the AD cmdlets where sometimes the script block syntax doesn't work the way you expect it to.

September 1, 2015 at 7:18 am

That work, thanks!

Question, why is the "$" required outside of the ($line.description)?

September 1, 2015 at 7:21 am

That's a sub-expression operator, and is what you use in strings when you want to expand something more complex than just a simple variable name. Otherwise, $line would get expanded, and the text ".Description" would become a literal part of the string, which is not what we want.