Jan
Jan

Reputation: 45

Get computer list from OU using Get-ADComputer cmdlet

I´m struggling with getting a list of computers from a specific OU via the Get-ADComputer cmdlet. The OU contains only computers. I specify the OU where the computers are placed and then I try to filter the objects based on hostname.

I want to list all computers whose hostname begins with "PC100". No matter what comes after the second zero.

The code:

Get-ADComputer -SearchBase "OU=PC,OU=LAB,DC=test,DC=cz" -Filter * | Select-Object name | where -Property Name -Match "PC100*"

I went through plenty of articles but haven't figured it out yet. PowerShell doesn't return any error, it simply doesn't generate any output.

Upvotes: 0

Views: 7378

Answers (1)

Petar Marinković
Petar Marinković

Reputation: 126

Try this one

Get-ADComputer -SearchBase "OU=PC,OU=LAB,DC=test,DC=cz" -Filter {Name -eq "PC100"} -Properties Name | Select-Object Name

This assumes that PC organizational unit is nested under LAB, and that the name of the computer is exactly PC100. If you don't know the exact name and you know that it PC100 is part of the name, change it to -Filter {Name -like "*PC100*"}

And don't pull all properties when you don't need them, it can cause potential performance issues

Upvotes: 1

Related Questions