Share this page : facebooktwitterlinkedinmailfacebooktwitterlinkedinmail
Mirror OU structure to the Folder structure

Some times, you create same OU structure as the folder structure, you can use below scripts to automate this:

Example: In the D drive,you have folder called Departments, you want the subfolder names are same as the OU names underĀ OU=Departments,OU=Users,OU=frankfu,DC=click.

get-childitem -Path "E:\Data\Departments" |`
foreach {new-ADOrganizationalUnit -name $_.Name -path "OU=Departments,OU=Users,OU=frankfu,DC=click"}

 

Find orphaned computer object and delete them based on last logon date

You may have a big number of computer objects which have been decommissioned, a good way is find it by the lastlogondate

Let’s say you want to delete the computer objects whose last logon date was more than half year ago:

$orphaned_Date=get-date (Get-Date).addDays(-180)

Get-ADComputer -Filter * -SearchBase "DC=domain,DC=com" -properties lastlogondate | where {$_.lastlogondate -le $orphaned_Date} | | Remove-adcomputer -Confirm:$false

Please keep in mind lastlogondate is not in the property by default, need -properties lastlogondate to show it.