Lukas
Lukas

Reputation: 35

Powershell script is slow to find computers

I have a script that is supposed to search for computers in different OUs with the same name in AD. eg.

Get-ADComputer -filter * -Searchbase "OU=domain,DC=home,DC=com"  -properties * |
    Where-Object {$_.DistinguishedName -like "*XXX09*"} |
        Select name, DistinguishedName

Everything works fine, but it is terribly slow, is there any way to speed it up, or build the script differently ?

Upvotes: 0

Views: 619

Answers (2)

Theo
Theo

Reputation: 61093

Not only can you speed-up this by using a filter, but also, using -Properties * is asking for ALL properties. That is useless and time consuming in this case because you only want to retrieve the Name and DistinguishedName.

Get-ADCumputer by default already returns these properties:
DistinguishedName, DNSHostName, Enabled, Name, ObjectClass, ObjectGUID, SamAccountName, SID, UserPrincipalName.

Try

Get-ADComputer -Filter "DistinguishedName -like '*XXX09*'" | Select-Object Name, DistinguishedName

Upvotes: 4

Peter the Automator
Peter the Automator

Reputation: 918

Use the filter during the search instead of after will reduce the query time quite a bit.

Get-ADComputer -filter 'DistinguishedName -like "*XXX09*"' -Searchbase "OU=domain,DC=home,DC=com" -properties * | select name, DistinguishedName

You might need to tune the query slighty, but i tested it with 'name' instead of 'DistinguishedName' and that works just fine (and quite a bit quicker ;))

Upvotes: 0

Related Questions