What domain controllers are computers authenticating to?

I am trying to write script below that will list the domain controller all computers in an OU are using for authentication and export the data to a file. Scripting is not my wheelhouse so I thought I would enlist some assistance.

Get-ADComputer -filter * -searchbase “OU=yourcomputerOU,DC=domain,DC=com” | %{(Get-WmiObject -Class win32_ntdomain -Filter “DomainName = ‘mydomain’” -ComputerName $_.Name).DomainControllerName}


Hmmm … why to make the life harder than needed?

Get-ADDomainController -Filter * | Select-Object -Property Name

Get-ADDomainController -filter * will return all domain controllers based on the machine used. It won’t return the authenticating DC for a remote machine. You’d also need to have the AD cmdlets on all machines to be able to use that approach

Ahh … now I actually understood what the question was about. Sorry, I got this wrong.


Do I insert your script at the beginning of my mine and leave the remaining scrip as is?

You could use $env:logonserver variable

Something like this maybe (I have not tested it)

Invoke-Command -ComputerName (Get-ADComputer -filter * -searchbase “OU=yourcomputerOU,DC=domain,DC=com”) -scriptblock {$env:logonserver}

or get the computer names in a text file and do this

Invoke-Command -ComputerName (gc C:\computers.txt) -scriptblock {$env:logonserver}