Wednesday, December 23, 2020

Get-ADComputer Powershell Custom Expression Pipeline

You may have to periodically run reports on where the computer is in AD but can't sort by the OU with the DistinguishedName containing the computer name.

$list = Get-content "C:\list\final.txt"

Foreach ($PC in $list){Get-ADComputer $PC | select Name, @{Name = 'AD OU'; Expression = {$_.DistinguishedName -replace "CN=$PC," } } }


Here's the same but for Windows 7 machines replacing it real time.

get-adcomputer -filter {Operatingsystem -like "Windows 7*"} -Properties * | select Name, OperatingSystem, @{Name = 'AD OU'; Expression = {$_.DistinguishedName -replace "CN=" + $_.Name + ","} }