ITGeeksHub

Import Active Directory Users, Groups, and OUs from CSV

The content discusses how to efficiently import users, groups, and organizational units (OUs) into Active Directory using a PowerShell script from a CSV file. It emphasizes transforming manual tasks into automated processes, allowing IT professionals to concentrate on innovation, along with providing instructions and prerequisites for successful execution.

“Streamlining the process of importing users, groups, and OUs from a CSV into Active Directory transforms tedious manual tasks into efficient automation, enabling IT professionals to focus on innovation rather than administration.”

Import Active Directory Users, Groups, and OUs from CSV using PowerShell

Import Active Directory Users, Groups, and OUs from CSV

Overview

This guide provides instructions on how to import Active Directory users, groups, and organizational units (OUs) from a CSV file using a PowerShell script. The script will create the specified OUs, groups, and users, and will add users to their respective groups.

CSV File Format

Below is the expected format for the CSV file to be used:

Sample CSV File

OUName,GroupName,UserName,Password
Sales,SalesGroup1,user1,P@ssword1
Sales,SalesGroup1,user2,P@ssword2
Engineering,EngineeringGroup1,user3,P@ssword3
Engineering,EngineeringGroup2,user4,P@ssword4
        

In this example, the first column is the Organizational Unit (OU) name, the second column is the group name, the third is the username, and the fourth is the password for the user.

PowerShell Script

Below is the PowerShell script to import the data from the CSV file:

# Import Active Directory Module
Import-Module ActiveDirectory

# Path to your CSV file
$csvPath = "C:\Path\To\Your\File.csv"

# Import CSV data
$data = Import-Csv -Path $csvPath

# Create OUs, Groups, and Users
foreach ($entry in $data) {
    # Create OU if it does not exist
    if (-not (Get-ADOrganizationalUnit -Filter "Name -eq '$($entry.OUName)'" -ErrorAction SilentlyContinue)) {
        New-ADOrganizationalUnit -Name $entry.OUName -Path "DC=yourdomain,DC=com"
        Write-Host "Created OU: $($entry.OUName)"
    }

    # Create Group if it does not exist
    if (-not (Get-ADGroup -Filter "Name -eq '$($entry.GroupName)'" -ErrorAction SilentlyContinue)) {
        New-ADGroup -Name $entry.GroupName -GroupScope Global -Path "OU=$($entry.OUName),DC=yourdomain,DC=com"
        Write-Host "Created Group: $($entry.GroupName)"
    }

    # Create User
    New-ADUser -Name $entry.UserName -GivenName $entry.UserName -Surname "User" -SamAccountName $entry.UserName -UserPrincipalName "$($entry.UserName)@yourdomain.com" `
               -Path "OU=$($entry.OUName),DC=yourdomain,DC=com" -AccountPassword (ConvertTo-SecureString $entry.Password -AsPlainText -Force) -Enabled $true
    Write-Host "Created User: $($entry.UserName)"

    # Add User to Group
    Add-ADGroupMember -Identity $entry.GroupName -Members $entry.UserName
    Write-Host "Added User: $($entry.UserName) to Group: $($entry.GroupName)"
}
        

Prerequisites

  • You must have the Active Directory PowerShell module installed.
  • Run the PowerShell script with administrative privileges.
  • Modify the path to the CSV file and the domain components in the script.

Execution Instructions

  1. Create a CSV file with the content provided above, and save it as ImportAD.csv.
  2. Modify the PowerShell script by replacing DC=yourdomain,DC=com with your own Active Directory domain components.
  3. Save the PowerShell script with a .ps1 extension (e.g., ImportADUsers.ps1).
  4. Open PowerShell as an administrator and execute the script using the command:
  5.             .\ImportADUsers.ps1
                

Leave a Reply