Managing Active Directory objects often requires moving computers between different OUs. This script uses PowerShell and the Active Directory module to automate the process, making it easier and faster to reorganize your directory structure.
PowerShell Script
Import-Module ActiveDirectory
$MoveList = Import-Csv -Path "d:\Temp\MauvaiseOU.csv" -Delimiter ","
ForEach ($item in $MoveList){
# my CSV contains 2 columns "Device Name" and "PAYS"
$Computer = $item.'Device Name'
$TargetOU = 'OU=Portable,DC=leblogosd,DC=lan'
Write-Host "Moving $Computer to $TargetOU"
Get-ADComputer $Computer | Move-ADObject -TargetPath $TargetOU
}
CSV File Format
The CSV file should have the following format:
Device Name,PAYS
Computer1,France
Computer2,Germany
Computer3,USA
Steps to Use the Script
1. Prepare the CSV File
Create a CSV file with the computer names and their respective target OUs. Ensure the file is saved at a location accessible to the script.
2. Open PowerShell
Open PowerShell with administrative rights. Type PowerShell
in the search bar, right-click on Windows PowerShell, and select Run as administrator.
3. Run the Script
Copy the script into a PowerShell file, e.g., MoveComputers.ps1
, and modify the path to your CSV file and target OU if necessary. Run the script in PowerShell.
.\MoveComputers.ps1
0 Comments