customer relationships

Active Directory : Create test users using PowerShell

I’ve often had a need for some users for various test purposes, and want a script to quickly create these users.

Create Organizatiol Unit (OU)

In order to use the cmdlets needed you need Access to a Domain Controller or have RSAT (opens a Google search) installed on your Workstation and use import-module *active*

You can skip importing the module if you are working on Server 2012 or Windows 8 as PowerShell 3.0 automatically imports needed modules.

I am not going to protect my Ou from deletion, as it just makes it more difficult to delelte afterwards. You might not want to do this, in order to simulate production environment.

First I define the name of the OU I want to create, next I store the Domain DN in a varialbe and finally I create the OU with protection against deletion  disabled.

# Define variable for Target root OU we are creating for our users.
$OUName = "UsersTestOU"
#
# Retrieve Domain DN and store in $DomainDN
$Domain = Get-ADDomain
$DomainDN = $Domain.DistinguishedName
#
# Create the new OU for our Test Users
New-ADorganizationalUnit -Name $OUName -Path $DomainDN `
-ProtectedFromAccidentalDeletion:$false

Create test users

First I define the number of test users I want to create, using the range operator in an array and then use Foreach-Object to create the number of users I defined.

1..10 | Foreach-Object {
New-ADUser -Name "TestUser$_" -Path "OU=$ouname,$domaindn"
}

We now have a new OU with 10 test users:

01-testUsers

Script for both steps:

# CreateTestOUandTestUsers.ps1
# Define variable for Target root OU we are creating for our users.
$OUName = "UsersTestOU"
#
# Retrieve Domain DN and store in $DomainDN
$Domain = Get-ADDomain
$DomainDN = $Domain.DistinguishedName
#
# Create the new OU for our Test Users
New-ADorganizationalUnit -Name $OUName -Path $DomainDN `
-ProtectedFromAccidentalDeletion:$false
#
# Create the users in the defined OU. Modify the range operator to adjust numbers of users.
1..10 | Foreach-Object { New-ADUser -Name "TestUser$_" `
-Path "OU=$ouname,$domaindn" }

 


Got a project that needs expert IT support?

From Linux and Microsoft Server to VMware, networking, and more, our team at CR Tech is here to help.

Get personalized support today and ensure your systems are running at peak performance or make sure that your project turns out to be a successful one!

CONTACT US NOW
best vpn deal