Author Posts

June 6, 2017 at 4:32 pm

Hi All,

i have a small problem
simple script to find computer in AD

### Get-ADComputer by Name ###
### C:\Users.txt - user names

$username = Get-Content "C:\Users.txt"
foreach ($user in $username)
{
Get-ADComputer -Filter $("Description -like '*$user*'") -Property Name,Description,OperatingSystem | Select Name,Description,OperatingSystem | 
Export-Csv "C:\Users.csv" -Append -NoTypeInformation
}

this part working Ok, just one things
if it didn't find description in AD it will remove it from list
My question, how i can have the same list in scv and if its can't find in AD it will say not in AD

Thanks

June 6, 2017 at 5:41 pm

This could be a starting point for you:

$username = Get-Content "C:\Users.txt"
foreach ($user in $username)
{
    $SearchHit = Get-ADComputer -Filter $("Description -like '*$user*'") -Property Name,Description,OperatingSystem 
    If($SearchHit){
        $output=[PSCustomObject]@{
            'Name' = $SearchHit.Name
            'Description' = $SearchHit.Description
            'OperatingSystem' = $SearchHit.OperatingSystem 
        }
    }
    Else{
        $output=[PSCustomObject]@{
            'Name' = $user
            'Description' = 'Not in AD'
            'OperatingSystem' = 'Not in AD'
        }
    }
    $output | 
    Export-Csv "C:\Users.csv" -Append -NoTypeInformation
}

(untested!!)

June 6, 2017 at 6:32 pm

Hi Olaf,

Thank you, this is what i was looking for
one more things, if in list i have name like O'laf or B'rad it throwing error
how to avoid this

Thanks

June 6, 2017 at 7:25 pm

Change

"Description -like '*$user*'"

to

"Description -like ""*$user*"""

June 6, 2017 at 7:33 pm

Thank you Olaf

why its not working in ADUser, i try modify your script
to

$username = Get-Content "C:\Users.txt"

foreach ($user in $username)
{
    
$SearchHit = Get-ADUser -Filter ("cn -Like '*$user*'") -Property Name,SamAccountName,mail
 If($SearchHit){
        $output=[PSCustomObject]@{
            'Name' = $SearchHit.Name
            'SamAccountName' = $SearchHit.SamAccountName
            'Mail' = $SearchHit.mail 
        }
    }
    Else{
        $output=[PSCustomObject]@{
            'Name' = $user
            'SamAccountName' = 'Not in AD'
            'Mail' = 'Not in AD'
        }
    }
    $output | 
Export-Csv "C:\Users.csv" -NoTypeInformation
}

but its not working this way

Thanks again for your help

June 6, 2017 at 7:36 pm

Do you get errors? I already have a suspision ... 😉

June 6, 2017 at 7:39 pm

No, no error
in csv file just one last name from the list

i just removed from code -Append

June 6, 2017 at 7:56 pm

OK ... try to change

-Filter ("cn -Like '*$user*'")

to

-Filter $("cn -like ""*$user*""")

June 8, 2017 at 2:00 pm

Hi Olaf,
still not working
no errors but it in output file just last name from list

June 8, 2017 at 2:05 pm

So you have to add the "-append" again. Otherwise every new entry will overwrite the last one.

June 8, 2017 at 2:13 pm

Thank you 😉