0

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 ?

1
  • Try to use the filter. Get-ADComputer -Filter 'Name -like "Computer01*" Commented Feb 18, 2021 at 9:58

2 Answers 2

4

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Most books on PowerShell that go into even the basics of performance issues recommend this; you should have your query return the fewest possible objects and the minimum needed fields from those object.
0

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 ;))

2 Comments

It does not work, it does not display any results. I tried the same thing ;)
I dont think you can filter on DistinguishedName... So either you use Name, or youll have to live with the longer queue times. sorry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.