Microsoft Powershell – Move Computer Accounts to OU based on csv-file

Release date: December 26th 2022

Welcome to my Microsoft Tips & Tricks section. I recently experienced a complete hardware crash in my old lab, and had to do a complete rebuild. Although this was sad, it gave me the opportunity to do it by scripting this time. This involved recreating the servers and joining them to the domain. Once that was done, I needed to move the Computer Accounts from the Computers OU to their respective OU’s, using a csv-file.

The script below uses a csv file with the following data:

  • CN (Computer Account)
  • OU (Organizational Unit)

Next, using the script below, I easily moved the Computer Accounts into the correct OU’s. NOTE: The user I ran this script with had Domain Admin permissions, otherwise this wouldn’t have worked.

Import-Module -Name ActiveDirectory

$computers = Import-Csv "<Path to csv-file>.csv" -Delimiter ";"
foreach ($computer in $computers)
    Get-ADComputer $computer.CN | Enable-ADAccount | Move-ADObject -TargetPath $computer.OU

Now that I had all the Computer Accounts in the right places, I could proceed with importing the GPO’s I have previously exported with Powershell, detailed here:

Microsoft Tips & Tricks section

Disclaimer: Every tips/tricks/posting I have published here, is tried and tested in different it-solutions. It is not guaranteed to work everywhere, but is meant as a tip for other users out there. Remember, Google is your friend and don’t be afraid to steal with pride! Feel free to comment below as needed.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: