Bulk move AD users to another OU with PowerShell (2024)

You already learned how to bulk convert user mailbox to shared mailbox. This time we like to bulk move AD objects to a new OU (Organizational Unit). There are more than a hundred of shared mailboxes across Active Directory (AD). If we search for the shared mailboxes in AD and move them to the OU, it takes time. This is an excellent way to automate the task with PowerShell. In this article, you will learn how to bulk move AD users with PowerShell to another OU from CSV file.

Table of contents

  • Before you start to bulk move AD users
    • Create a target OU in Active Directory
    • Get the distinguished name
    • Create CSV file with AD users
    • Check the content in CSV file
  • Bulk move AD users to another OU PowerShell script
  • Bulk move AD users to another OU with CSV file
    • Verify the result
  • Conclusion

Before you start to bulk move AD users

If you have a list of AD users in one OU, you can select the AD objects and move them in Active Directory Users and Computers. Yes, it’s that simple to drag and drop. What if you have users in different OUs? In our case, there are over a hundred shared mailboxes. It will take a lot of time to search and move them. That’s why we have to work smart and make use of PowerShell.

Create a target OU in Active Directory

Create a target OU if you don’t have one. We recommend to keep it tidy and move all the shared mailboxes to the target OU SharedMailbox.

Bulk move AD users to another OU with PowerShell (1)

Get the distinguished name

You can get the OU distinguished name in Active Directory. Right-click the target OU and click Properties. Go to the Attribute Editor tab. Find the attribute distuingedName in the attributes list. Double-click to open the string and copy the value. You will need it in the next part.

Bulk move AD users to another OU with PowerShell (2)

You can alsoget the Organizational Unit distinguished name with PowerShell.

Create CSV file with AD users

Before we can run the script, we need to have a CSV file with the AD users SamAccountName. In our example, the shared mailboxes are going to be moved. When you create a mailbox, an AD object is automatically created in AD.

Bulk move AD users to another OU with PowerShell (3)

Run Exchange Management Shell as administrator. The first cmdlet will give us the result. The second cmdlet will export the result to a CSV file.

[PS] C:\>Get-Mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"} | Select Name, SamAccountName, OrganizationalUnit, DistinguishedNameName SamAccountName OrganizationalUnit DistinguishedName---- -------------- ------------------ -----------------Sales sales exoip.local/Company/Exchange/SharedMailbox CN=Sales,OU=SharedMailbox,OU=Exchange,OU=Company,DC=exoip,DC=localInfo info exoip.local/Company/Exchange/SharedMailbox CN=Info,OU=SharedMailbox,OU=Exchange,OU=Company,DC=exoip,DC=localHR hr exoip.local/Company/Exchange/SharedMailbox CN=HR,OU=SharedMailbox,OU=Exchange,OU=Company,DC=exoip,DC=local[PS] C:\>Get-Mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"} | Select Name, SamAccountName, OrganizationalUnit, DistinguishedName | Export-Csv -Path "C:\temp\shared_mailboxes.csv" -NTI

After exporting the CSV file, the next step is to read the CSV file content with PowerShell.

Check the content in CSV file

Verify that the CSV file is readable in PowerShell.

[PS] C:\>Import-Csv "C:\temp\shared_mailboxes.csv"Name SamAccountName OrganizationalUnit DistinguishedName---- -------------- ------------------ -----------------Sales sales exoip.local/Company/Exchange/SharedMailbox CN=Sales,OU=SharedMailbox,OU=Exchange,OU=Company,DC=exoip,DC=localInfo info exoip.local/Company/Exchange/SharedMailbox CN=Info,OU=SharedMailbox,OU=Exchange,OU=Company,DC=exoip,DC=localHR hr exoip.local/Company/Exchange/SharedMailbox CN=HR,OU=SharedMailbox,OU=Exchange,OU=Company,DC=exoip,DC=local

Bulk move AD users to another OU PowerShell script

Download the Move-ADUsers.ps1 PowerShell script or copy and paste the below code in Notepad. Give it the name Move-AdUsers.ps1 and place it in the C:\scripts folder. Create a scripts folder if you don’t have one.

# Import AD ModuleImport-Module ActiveDirectory # Import the data from CSV file and assign it to variable$Import_csv = Import-Csv -Path "C:\temp\shared_mailboxes.csv"# Specify target OU where the users will be moved to$TargetOU = "OU=SharedMailbox,OU=Exchange,OU=Company,DC=exoip,DC=local" $Import_csv | ForEach-Object { # Retrieve DN of User $UserDN = (Get-ADUser -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"
  • Line 5: Edit the CSV file name and the path.
  • Line 8: Edit the target OU.

In the next step, we will have a look at the bulk move AD Users PowerShell script.

Bulk move AD users to another OU with CSV file

Run the PowerShell script to bulk move AD users to another OU. Good to know is that there is a -WhatIf parameter added to the script. If you run the script, nothing will happen in the environment. You will get an output showing what will happen.

[PS] C:\>cd C:\scripts[PS] C:\scripts>.\Move-ADUsers.ps1Moving Accounts.....What if: Performing the operation "Move" on target "CN=Sales,OU=Company,DC=exoip,DC=local".Moving Accounts.....What if: Performing the operation "Move" on target "CN=Info,OU=Company,DC=exoip,DC=local".Moving Accounts.....What if: Performing the operation "Move" on target "CN=HR,OU=Company,DC=exoip,DC=local".Completed move

Everything is looking good. We can remove the -WhatIf parameter and rerun the PowerShell script.

[PS] C:\scripts>.\Move-ADUsers.ps1Moving Accounts.....Moving Accounts.....Moving Accounts.....Completed move

Verify the result

Have a look at Active Directory Users and Computers. The shared mailboxes in the CSV file are moved to another OU. In this case, the SharedMailbox OU.

Bulk move AD users to another OU with PowerShell (4)

Another check, but this time with PowerShell.

[PS] C:\>Get-Mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"} | Select Name, SamAccountName, OrganizationalUnitName SamAccountName OrganizationalUnit---- -------------- ------------------Sales sales exoip.local/Company/Exchange/SharedMailboxInfo info exoip.local/Company/Exchange/SharedMailboxHR hr exoip.local/Company/Exchange/SharedMailbox

Everything is looking great. Did this help you to bulk move AD objects to another OU?

Read more: Create bulk mailboxes in Exchange Server with PowerShell »

Conclusion

In this article, you learned how to bulk move AD users to another OU with PowerShell. Download the Move-ADusers.ps1 PowerShell script, edit the CSV path, and the target OU. Run the script and verify that the AD objects are moved successfully to the new OU.

Did you enjoy this article? You may also like Change Users UPN with Powershell. Don’t forget to follow us and share this article.

Bulk move AD users to another OU with PowerShell (2024)

FAQs

How do I move multiple users from one OU to another? ›

How to move User accounts between OUs? First, select the OU to which you want to move the users. Then, select the list of users that need to be moved by either importing a CSV file or by using the search option, and finally click on apply.

How do I move AD users in PowerShell? ›

Windows PowerShell
  1. Identify the domain in which the user to be moved is located.
  2. Create and compile the script for moving an AD user from one OU to another. Execute the script in PowerShell.
  3. Sample script for moving user account in AD.
  4. To move AD users in bulk, you will have to write another script.

How to bulk import AD users with PowerShell from a CSV file? ›

How to import users into Active Directory from CSV file
  1. Step 1: Create CSV file with users. Without a CSV file, you can't use the script and import the users into AD. ...
  2. Step 2: Prepare import AD users PowerShell script. Download and place Import-ADUsers. ...
  3. Step 3: Run import AD users PowerShell script. ...
  4. Step 4: Verify AD users.
Feb 11, 2024

How to move bulk ad users to different OU through CSV file? ›

Simply follow the instructions and get migrated.
  1. $adUsers = Import-Csv -Path “C:\PowerShell\moveuser.csv” $TargetOU = “OU=Marketing,DC=VMLAB,DC=LOCAL” ...
  2. # Get ad user. $aduser = (Get-ADUser -Identity $_.SAMAccountName).distinguishedName.
  3. # Move user to target OU. Move-ADObject -Identity $aduser -TargetPath $TargetOU.
Oct 4, 2023

How to move a computer from one OU to another in Active Directory? ›

Solution
  1. Open the Active Directory Users and Computers snap-in.
  2. If you need to change domains, right-click on “Active Directory Users and Computers” in the left pane, select Connect to Domain, enter the domain name, and click OK.
  3. In the left pane, browse to the OU you want to move.
  4. Right-click on the OU and select Move.

What is the PowerShell command to list all users in an AD group? ›

You can just type the Get-ADGroupMember cmdlet in a PowerShell window and you'll be prompted to enter the name of the group you want to use. Note that you must be logged in to Windows with a domain user account that has permission to read the group(s) you want to list.

How to import bulk users in Active Directory using PowerShell? ›

Windows PowerShell

Create the CSV file with the list of all users to be imported to AD, with the LDAP names of the necessary attributes as the header. Identity the domain and the OU to which the users have to be imported. Create the script using the Add-ADGroupMember cmdlet, and execute it in the PowerShell window.

What is the PowerShell command to move to another directory? ›

The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another location. The locations must be supported by the same provider. For example, it can move a file or subdirectory from one directory to another or move a registry subkey from one key to another.

How to move bulk computer to another OU? ›

Procedure: First, select the OU to which you want to move the computers. Then, select the list of computers that need to be moved by either importing a CSV file or by using the search option, and finally click on apply.

How do I Import all users from Active Directory? ›

To enable AD user sync, follow the below steps:
  1. Select the Enable Active Directory Sync checkbox.
  2. Choose a preferred Recurrence Type: Daily, Weekly, or Monthly.
  3. Enter a Start Time and Start Date for the schedule.
  4. Click Import to start the import immediately. The AD sync schedule will also be created.

How do I export all AD users to CSV in PowerShell? ›

Running a PowerShell Script:
  1. Run the following script changing the export path as required: $ExportPath = 'c:\adusers_list.csv' Get-ADUser -Filter * | Select-object DistinguishedName,Name,UserPrincipalName | Export-Csv -NoType $ExportPath.
  2. Open the CSV file generated from the script in Excel to see the results:
Jun 23, 2023

How to bulk add users to Active Directory? ›

While adding multiple users to Active Directory, who have the same set of permissions, you can create a user template by specifying the required permissions and create a CSV file containing the names of the users. You can then import the CSV file and apply the template to create users in bulk.

How do I export all ad users from specific OU? ›

To export users from specific OUs use the “-SearchBase” command and the “distinguishedName” value of the OU. In the below example I'm getting all the users in my accounting OU. Here is the PowerShell output. Then add export-csv -path to the end to export this to CSV.

How do I add multiple users to Exchange Admin Center? ›

Add multiple users in the Microsoft 365 admin center

In the admin center, choose Users > Active users. Select Add multiple users.

How do I pull all users from an OU in PowerShell? ›

List All Users from an OU with PowerShell
  1. Open PowerShell.
  2. Copy and paste the command below. You will need the distinguishedName of the OU, see details below.
  3. Open the AD Pro Toolkit.
  4. Click on User Reports – > All users.
  5. Click Browse to select one or more OUs.
  6. Click Run.
Dec 5, 2023

How do I link an existing group policy to another OU? ›

Link the GPO to the Specific OU: Navigate to the desired OU in the Group Policy Management Console. Right-click on this OU and select “Link an Existing GPO”. In the “Select GPO” dialog under Group Policy Objects, select the GPO you want to link and click OK.

Top Articles
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6193

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.