This PowerShell script shows how to determine the OU the local computer belongs to.
Some of IT admins want to verify that is has been deployed to the correct OU. So this script can help do that.
Step1: Start the PowerShell Console with administrator. To run the script in the Windows PowerShell Console, type the one command< Script Path> at the Windows PowerShell Console.
For example, type C:\Script\ComputerOUBelongsTo.ps1
The step is shown in the following figure:

When the script finishes running, the results is shown in the following figure:

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.
$DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher
$DirectorySearcher.Filter = $Filter
$SearcherPath = $DirectorySearcher.FindOne()
$DistinguishedName = $SearcherPath.GetDirectoryEntry().DistinguishedName
$OUName = ($DistinguishedName.Split(","))[1]
$OUMainName = $OUName.SubString($OUName.IndexOf("=")+1)
$DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher $DirectorySearcher.Filter = $Filter $SearcherPath = $DirectorySearcher.FindOne() $DistinguishedName = $SearcherPath.GetDirectoryEntry().DistinguishedName $OUName = ($DistinguishedName.Split(","))[1] $OUMainName = $OUName.SubString($OUName.IndexOf("=")+1)
Windows PowerShell 2.0