I have a script to run when a user is terminated. One thing the script does is move the user to a ‘Terminated Users’ OU.
There are multiple ‘Terminated Users’ OU depending on what part of the company the user works for.
For example
CN=John Smtih,OU=Users,OU=Head Office,OU=AMF,OU=Macquarie Leisure,DC=mlol,DC=local
Would need to be moved into
CN=John Smith Kiosk,OU=Terminated Users,OU=AMF,OU=Macquarie Leisure,DC=mlol,DC=local
And
CN=Bob Tan,OU=Users,OU=Studios,OU=HYPOXI USA,OU=Macquarie Leisure,DC=mlol,DC=local
Would go to
CN=Bob Tan,OU=Terminated Users,OU=HYPOXI USA,OU=Macquarie Leisure,DC=mlol,DC=local
I have the following but it only works for some of the OUs
Set specific Terminated Users OU $DN = (Get-ADUser $Username).distinguishedname $newDN = $DN -replace 'ou=users,ou=w+', 'OU=Terminated Users' $newOU = $newdn -replace '^[^,]+,' Move user to Terminated Users OU Get-ADUser $Username | Move-ADObject -TargetPath $newOU
Any ideas on why? Or another method to doing this?
submitted by /u/ardentscript
[link] [comments]
The post Moving users to a new OU appeared first on How to Code .NET.