There is a way we can check an OU object exists or not in Active directory, using a simple powershell script we can query, here i this article we write the code for the same.
Generally, Active Directory admins maintain the list of AD objects and do the cleanup activity to meet the compliance standards. I’m going to query an AD OU as “OU=Retail DC=CitrixTech,DC=COM”.
function Check-AD-OU {
[cmdletbinding()]
param(
[string] $OuName
)
try {
if([ADSI]::Exists("LDAP://$OuName")) {
Write-Host "OU exists in AD"
}
else {
Write-Host "OU doesn't exist"
}
} catch
{
Write-Host "Something Went Wrong"
}
}
How to use this script with example.
#Example one
Check-AD-OU -Ouname "OU=Retail DC=CitrixTech,DC=COM"
OutPut
OU exists in AD
#Example two
Check-AD-OU -Ouname "OU=Sales DC=CitrixTech,DC=COM"
We have also written other articles to manage Active directory, like moving the AD objects from one OU to another OU and getting computer objects from a specified OU, one search the article and use them on a daily basis.
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply