TECHDIIP

  • Tech
  • Windows
  • Powershell
  • Scripts
  • News
  • Games

Powershell check OU exists or not in Active Directory

27 June 2020 admin Leave a Comment

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.

Powershell

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Techdiip © 2012 - 2020 | Please Read Our

  • Privacy Policy
  • Disclaimer – User Agreement
  • About Us
  • Contact Us