Bulk Move Active Directory Computer Accounts to a New OU Using PowerShell

Active Directory estates change all the time. You might be:

  • moving devices into a new OU for a rollout

  • separating machines by site, department, or build type

  • tidying up after a refresh project

  • preparing a clean OU structure for new Group Policy or security rules

Doing this manually in Active Directory Users and Computers works for a handful of devices, but it quickly becomes slow and error-prone when you have dozens (or hundreds).

This PowerShell script lets you move a batch of computer accounts into a target OU using a simple text file of hostnames. You provide a list of computer names, set the OU path, and the script handles the rest.


Why people need this script

You might use this when:

  • you need to move computers into an OU that has a new GPO linked

  • you are re-structuring OUs by region, site, or client

  • you are building a “staging OU” for testing policies before wider rollout

  • you want a repeatable process your team can run consistently

If you are working heavily with OU structure and policies, this pairs well with PowerShell Startup Scripts Using GPO and Copy Files and Folders to User Computers via GPO, because OU placement often decides what policies and deployments a machine receives.


What this script does

At a high level, the script:

  1. Reads hostnames from a text file

  2. Looks up each computer account in Active Directory

  3. Moves the computer account into a target OU

  4. Prints a success message for each move

  5. Prints a failure message if a computer cannot be found or moved


Before you run it

1) Make sure you have permissions

You will need rights to move computer objects into the target OU. If you do not, you will see access denied errors.

2) Ensure the Active Directory module is available

Run it on:

  • a domain-joined admin machine with RSAT installed, or

  • a server where the AD tools are installed

3) Create your hostname list

Create a text file containing one computer name per line, for example:

PC-001
PC-002
LAPTOP-103
LAPTOP-104

The two lines you must change

Because OU paths and naming can identify a company, I have replaced them with safe example values.

You must update these in your environment:

  • $TargetOU → change to your real OU distinguished name

  • Get-Content -Path "C:\hostnames.txt" → change if your file is in a different location


Script

<#
.SYNOPSIS
This script moves Active Directory computer accounts to a specified organizational unit (OU).
.DESCRIPTION
The script:
- Reads a list of computer names from a text file (<code data-start="2794" data-end="2812">C:\hostnames.txt</code>).
- For each computer, attempts to retrieve its AD object using <code data-start="2881" data-end="2897">Get-ADComputer</code>.
- If the computer is found, it moves the computer object to a target organizational unit (OU).
- If an error occurs, it will display the error message.
.PARAMETER None
This script does not require any input parameters.
.INPUTS
A text file (<code data-start="3151" data-end="3169">C:\hostnames.txt</code>) containing a list of computer names to move.
.OUTPUTS
A message for each computer indicating whether the move was successful or failed.
.NOTES
Version: 1.0
Author: DigitalGeekery
Creation Date: 13/03/2025
Last Updated: 13/03/2025
Purpose/Change: Moves Active Directory computer objects to a target organizational unit.
.EXAMPLE
Run the script to move all computers listed in <code data-start="3578" data-end="3596">C:\hostnames.txt</code> to the target OU.
#&gt;

$TargetOU = "OU=Workstations,OU=Region,OU=Computers,DC=Example,DC=Local"

$ADComputers = Get-Content -Path "C:\hostnames.txt"

foreach ($computerName in $ADComputers) {
try {
if (($currentComputer = Get-ADComputer -Identity $computerName -ErrorAction Stop)) {
Move-ADObject -Identity $currentComputer -TargetPath $TargetOU -ErrorAction Stop
"OKAY: Moved $($computerName) to $TargetOU" | Out-Default
}
}
catch {
"FAIL: $($_.Exception.Message)" | Out-Default
}
}

How to run it safely

  1. Start with a small test list (2–5 machines)

  2. Confirm the devices appear in the new OU

  3. Confirm expected policies apply (or run a policy refresh)

If you are troubleshooting policy behaviour after the move, your post Fix Windows Log-On Issues by Repairing Critical Services and Forcing Group Policy can be a useful follow-on read.


Common issues and quick fixes

“Cannot find an object with identity…”

  • The hostname in the text file is wrong

  • The computer account does not exist

  • There are trailing spaces in the file

Tip: keep the list clean and use one name per line.

“Access is denied”

  • You do not have permission to move objects into that OU

  • OU delegation or protected objects are blocking it

“The directory service is unavailable”

  • You are not running it from a domain-joined context

  • DNS/connectivity issue to a domain controller

Total
0
Shares
Previous Post

Find and Clean Up Inactive Computers in Active Directory with PowerShell

Related Posts

How To Fix Error: There are Currently No Logon Servers Available

When a user logs on to a Windows computer that is joined to an Active Directory domain, they may see the following error message:There are currently no logon servers available to service the logon request.This message says that none of the AD domain controllers (LogonServer) is available from this computer to authenticate the user. The
Read More

How to Reserve IP Address on Windows Server DHCP?

DHCP reservation is the creation of a special entry on the DHCP server. Thanks to this, the same IP address from the DHCP scope address pool will be issued for a specific device (MAC address). In this article, we’ll look at how to create and manage reservations on a DHCP server running Windows Server 2019.
Read More