Tuesday, 9 May 2023

How to move bulk users one OU to another OU using PowerShell.

 

How to move bulk users one OU to another OU using PowerShell.


# What if   = Move user to target OU. Remove the -WhatIf parameter after you tested.

Test Script :-

 

 

$Import_csv = Import-Csv -Path "C:\temp\moveusers.csv"

 # Specify target OU where the users will be moved to

$TargetOU = "OU=ADMIN OU,OU=IT Accounts,OU=Support,OU=Administration,DC=abc,DC=com"

 $Import_csv | ForEach-Object {

     # Retrieve DN of User

    $UserDN = (Get-ADGroup -Identity $_.SamAccountName).distinguishedName

     Write-Host "Moving Accounts....."

     # Move user to target OU. Remove the -WhatIf parameter after you tested.

    Move-ADObject -Identity $UserDN -TargetPath $TargetOU -WhatIf

}

Write-Host "Completed move"


# Move user to target OU. Remove the -WhatIf parameter after you tested.

Final Script :-

 

 

$Import_csv = Import-Csv -Path "C:\temp\moveusers.csv"

 # Specify target OU where the users will be moved to

$TargetOU = "OU=ADMIN OU,OU=IT Accounts,OU=Support,OU=Administration,DC=abc,DC=com"

 $Import_csv | ForEach-Object {

     # Retrieve DN of User

    $UserDN = (Get-ADGroup -Identity $_.SamAccountName).distinguishedName

     Write-Host "Moving Accounts....."

     # Move user to target OU. Remove the -WhatIf parameter after you tested.

    Move-ADObject -Identity $UserDN -TargetPath $TargetOU 

}

Write-Host "Completed move"


 

No comments:

Post a Comment