Windows Server

Test-Environment: Building an Active Directory OU structure with Groups and Users with PowerShell Automation

You have just started learning something new and you are now about to bulid up a test environment with Windows Server ? For a good test environment, you will need a Domain-Controller and some OUs, Groups and users to play with. In this blog post I will provide a script for download that enables you to create these objects in just a few seconds, so you can start instantly.

The Script

Run the code below on your Domain-Controller, best in ISE oder Visual Studio Code.

Define OUs, groups and users of your choice in line 14-16. The script is neutral. Users are created in random OU. You do not have to enter a domain name, only rename the objects if desired.

Author: Patrick Gruenauer | Microsoft PowerShell MVP [2018-2022]
This script is inteded for use in a test environment. It creates OUs, 
Groups and Users. 

# If necessary, bypass the execution policy and suppress errors.

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force -Confirm:$false
$ErrorActionPreference = "SilentlyContinue"

# Define your OUs, Groups and Users here

$OUs = 'HR','Technicians','CEOs','Groups','Workstations'
$Groups = 'HR','Technicians','CEOs'
$usernames = @('Hans Womanizer','Tatjana Schlank','Birgit Langbein',
'Franz Bizeps','Bernd Bullseye','Michael Hightower','Patrick Cumbersome',
'Peter Travesty','Markus Haul','Tatjana Linux','Maria Azure','Susanne Amazon')

$root = $env:USERDNSDOMAIN.Split('.')[1]
$sub = $env:USERDNSDOMAIN.Split('.')[0]

# Create OUs

Foreach ($o in $OUs) {

New-ADOrganizationalUnit -Name $o -Verbose


# Create Groups

Foreach ($g in $Groups) {

New-ADGroup -Name $g `
-Path "OU=Groups,DC=$sub,DC=$root" `
-GroupScope Universal -GroupCategory Security -Verbose


# Create Users and place them into random OU. Add users to groups that match OU.

foreach ($u in $usernames) {

$split = $u.split(' ')
$randomou = Get-Random -InputObject 'HR','Technicians','CEOs'

$sam = ($split[0].Substring(0,1) + '.' + $split[1]).ToLower()
$upn = ($split[0].Substring(0,1) + '.' + $split[1] + '@' + $env:USERDNSDOMAIN).ToLower()

New-ADUser `
-Name $u `
-GivenName $split[0] `
-Surname $split[1] `
-DisplayName $u `
-Enabled $true `
-AccountPassword (ConvertTo-SecureString -AsPlainText '123user!' -Force) `
-SamAccountName $sam `
-UserPrincipalName $upn `
-Path "OU=$randomou,DC=$sub,DC=$root" `


# Add OU Users to Group

$ceos = "OU=CEOs,DC=$sub,DC=$root"
$hr = "OU=HR,DC=$sub,DC=$root"
$tech = "OU=Technicians,DC=$sub,DC=$root"

Get-ADUser -Filter * -SearchBase $ceos | 
ForEach-Object {Add-ADGroupMember -Identity CEOs -Members $_ -Verbose}

Get-ADUser -Filter * -SearchBase $hr | 
ForEach-Object {Add-ADGroupMember -Identity HR -Members $_ -Verbose}

Get-ADUser -Filter * -SearchBase $tech | 
ForEach-Object {Add-ADGroupMember -Identity Technicians -Members $_ -Verbose}

Start-Process dsa.msc

Thank you for reading this article and enjoy your new Active Directory test environment.

4 replies »

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.