Function Get-AdsiComputerCN([Parameter(ValueFromPipeline=$true)][String]$ComputerName = $env:ComputerName) {
Process {
$adsiSearcher = [adsisearcher]"(&(objectCategory=Computer)(name=$($ComputerName)))"
[void]$adsiSearcher.PropertiesToLoad.Add('canonicalname')
$adsiSearcher.FindOne().Properties.canonicalname
$adsiSearcher.Dispose()
}
}
You need Active Directory module installed .
Refer how to install RSAT for AD Powershell module - https://theitbros.com/install-and-import-powershell-active-directory-module/
Cheers