There are a lot of ways to get the OU of the current computer, but most don’t work if you are outside your home domain. This code does, without requiring AD cmdlets:
#My Computername works anywhere in forest $strFilter = "(&(objectCategory=Computer)(Name=$env:computername))" $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.Filter = $strFilter $searchRootName = [system.directoryservices.activedirectory.forest]::GetCurrentForest().Name.ToString() $SearchRoot = "GC://"+$SearchRootName $objSearcher.SearchRoot = $SearchRoot $objPath = $objSearcher.FindOne() $dn = ($objPath.GetDirectoryEntry()).Path $MyPCOU = $dn.Substring($dn.IndexOf("OU"),$dn.Length-$dn.IndexOf("OU")) $MyPCOU
Update: 9/23/2017: You can also get this information with the ADSystemInfo COM object, see Get-ADSystemInfo; Wrapping the ADSystemInfo ComObject.