You could try something like this in PowerShell to check that the named computer is in the OU or not:
Script:
import-module activedirectory
$OU = @()
$CheckOU = "LaptopOU"
$computerName = "Laptop12345"
$user = get-adcomputer $computerName -Properties *
$user.DistinguishedName -split "," | %{If($_ -match "OU="){$OU += $_ -replace "OU=",""}}
If($OU -match $CheckOU){
"Computer:$computerName is in the OU:$CheckOU"
# Do something...
}
Else{
"Computer:$computerName is not in the OU:$CheckOU"
# Do something else..
}
This will take a $computerName
and get all the OU's that it's in from Active Directory and stores them in an $OU
array.
Then you can use that array to simply check if the computer is in the given OU ($CheckOU
) or not by using the -match
operator.
Note: You need to make sure that you import the activedirectory
module. If you do not have this to import follow this link for how to get it: activedirectory module
For more Cmdlet's and syntax on the Powershell Active Directory module follow this Link