I just love the power of PowerShell and every day I challenge myself to automate things, today I needed to create 100 users in my Windows 2012 Lab. PowerShell 3.0 ISE make everything so easy with syntax highlighting, IntelliSense, more modules etc..

Fire up PowerShell ISE, and start looking in the command list on the right, example drivers

image

Anyway, back to my script, there is a command called New-ADUser, that sounds great doesn’t it?

What else, do we need? Domain, password and set some name attributes

#Import Active Directory module
import-module ActiveDirectory

#Set some variables for domain, where to create the users and the password
$ADdomain = “@lab.local”
$ADpath = “OU=Users,DC=youlabname,DC=local”
[System.Security.SecureString]$password = ConvertTo-SecureString -String 1ComplexPassword -AsPlainText -Force

#How many users to create?
$count = 1
while ($count -le 100)
{
    #This will actually create the users, all the users will be named “User XXX”
    New-ADUser -Name “User $count” -GivenName “User” -Surname $count -SamAccountName “User$count”  -DisplayName “User $count” -PasswordNeverExpires $true -AccountPassword $password -Enabled $true -UserPrincipalName “user$count$ADdomain” -Path $ADPath

    $count++
}

And a minute after, here are all the users

image

 

I have seen to many scripts like this, that will randomize real names, organization names, departments etc. but that is up to you.. Personally for my lab I just need many users with easy to remember names.

Happy scripting!

Advertisements