Windows – Querying Domain Controller objects using Powershell

active-directorypowershellwindows

Could someone explain to me why this does not work?

Import-Module ActiveDirectory 
$dcs = Get-ADComputer -Filter {DistinguishedName -Like "*Domain Controllers*"}

I get no results for this query.

Alternatively, could someone suggest a way using the module above that I can generate a list of systems on my domain that are NOT Domain Controllers (which is what I'm eventually trying to achieve).

Cheers

Best Answer

It looks like a bug to me. -like operator doesn't work with all properties. It doesn't work with DistinguishedName, SID, ObjectClass, but it works with Name, DSNHostName, SamAccountName...

The following command will give you all domain controllers:

PS C:> Get-ADComputer -SearchBase "OU=Domain Controllers,DC=test,DC=local" -Filter *

This command will give you all computers that are NOT domain controllers:

PS C:> Get-ADComputer -LDAPfilter "(&(objectCategory=Computer)(!userAccountControl:1.2.840.113556.1.4.803:=8192))"