Reputation: 7605
Function ListComputers
{
$strCategory = "computer"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
Write-Host $objDomain
$objSearcher.Filter = ("(objectCategory=$strCategory)")
$colProplist = "name"
foreach ($i in $colPropList)
{
$objSearcher.PropertiesToLoad.Add($i)
}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$objComputer = $objResult.Properties; $objComputer.name
}
}
This is searching the entire domain. How do I only search in a specific OU in the domain?
Upvotes: 1
Views: 2356
Reputation: 1217
There are a couple different ways. You can use Get-ADOrganizationalUnit
Get-ADOrganizationalUnit -Identity 'OU=Location,OU=Group,OU=UserAccounts,DC=STUFF,DC=COM'
Otherwise you can still use Get-ADComputer
Get-ADComputer -Filter '*' -SearchBase "OU=Workstations,OU=$City,DC=Fabrikam,DC=com"
Second example found here: Powershell script to query specifc OU in AD for computer names and export
Upvotes: 0
Reputation: 44
You could use:
Get-ADComputer -Filter * -SearchBase "CN=Workstations, DC=contoso, DC=com"
It might make it a bit easier?
Upvotes: 1