For today's cmdlet, we're going to focus on
Get-AdComputer. Similar to one of the previous cmdlets of the day Get-AdUser, this cmdlet is it's counterpart in the Active Directory module. Instead of retrieving user objects from Active Directory, this cmdlet finds computers in OUs.
If you're a beginner/intermediate PowerShell scripter, be sure to check out my FREE mini-course on Building a PowerShell Tool! 9,000+ words of deep explanation and insights on how to build a PowerShell tool.
The Get-AdComputer Filter Parameter
One way to use this cmdlet is to use the
Filter parameter. We can use the
Filter parameter to search for computers in Active Directory based on their name, for example.
Get-ADComputer -Filter "Name -like 'F*'"
Finding Computers in an OU
Perhaps we're not necessarily concerned with their name but in what OU they're in. Like the
Get-AdUser cmdlet, this cmdlet has a
SearchBase parameter we can use to limit the search only to an OU and all of its child OUs.
Below I'm finding all of the domain controllers inside of my mylab.local's Domain Controllers OU.
Get-ADComputer -Filter * -SearchBase 'OU=Domain Controllers, DC=mylab, DC=local'
But what if you've got a ton of child OUs under the parent OU and you'd like to only get computers from Active Directory in that parent OU? In that case, we can use the
Below I'm using the
1 argument to only find computer objects in the immediate OU. I could have used
2 instead to define a "subtree" which would recursively look through all of the child OUs as well.
Get-AdComputer -Filter * -SearchBase 'OU=Domain Controllers,DC=mylab,DC=local' -SearchScope 1
If you're good at LDAP filters, you can also use the
LDAPFilter parameter on
Get-ADComputer. The LDAP filter allows you to use LDAP syntax to hone in on exactly the computer you're looking for.
LDAPFilter can be used with the
SearchBase parameter or by itself.
Below, I'm using the LDAP filter to find all computers that start with F and are in the Domain Controllers OU.
Get-ADComputer -LDAPFilter "(name=f*)" -SearchBase "OU=Domain Controllers,DC=mylab,DC=local"
These are some of the most common use cases for this cmdlet of the day. For a full breakdown, check out the Microsoft documentation.
Join the Jar Tippers on Patreon
It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!Become a Patron!
Subscribe to Adam the Automator
Get the latest posts delivered right to your inbox