Location: Articles

Articles

Articles

Powershell 2.0 script to bulk create test user accounts

By Tony Murray on Monday, November 09, 2009 10:35 PM

This script uses Powershell 2.0 with the Windows Server 2008 R2 Active Directory service provider and CMDlets.  Use the script to bulk create test user objects in AD.

#########################################################
#
# Name: BulkCreateUsers.ps1
# Author: Tony Murray
# Version: 1.0
# Date: 10/11/2009
# Description: PowerShell script to bulk create users
#
#########################################################

### Variables

# Get the logged-on user's domain in DN form
$mydom = (get-addomain).distinguishedname

# Specify the OU we want to create the users in
$ouName = "User Accounts"

# Build the full DN of the target OU
$oudn = "OU=$ouname,$mydom"

# Specify the number of users to create
$userCount = 50

# Specify the description attribute for the users
$datetime = get-date -format G
$desc = "Test user created $datetime"

### Start doing things

# Check if the target OU exists. If not, create it.
$OU = get-adorganizationalunit -Filter { name -eq $ouname }
if($OU -eq $null)
{New-ADOrganizationalUnit -Name $OUName -Path $mydom}
else
{write-host "The OU" $ou "already exists - this is good..."}

# Create users

$i = 1
While ($i -le $usercount)
{
$Uname = "User" + $i
$UDdname = "Test User" + $i
New-ADUser –Name $Uname –SamAccountName $Uname –DisplayName $UDdname `
-Path $oudn –Enabled $true –ChangePasswordAtLogon $true -description $desc `
-AccountPassword (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -force) -PassThru
$i = $i + 1
}

#End

Comments

By addominali @ Wednesday, August 18, 2010 11:30 PM

thanks! really useful!


You must be logged in to post a comment. You can login here
Copyright 2009 ActiveDir.org
Terms Of Use