Partager via


Create Test Accounts With Powershell

When you’re setting up a SharePoint lab for development and testing, one of the things you should create is test bed of user accounts in your lab’s directory. Creating accounts by hand can be a very tedious, so instead look towards powershell to automate the task. Here’s a sample script that works with the Microsoft module for Active Directory Management:

 $secure = ConvertTo-SecureString 'Pass@word1' -asplaintext –force 

$username = “newtestuser”

$ADContainer = “OU=TestAccounts2,DC=VMLAB,DC=LOCAL”

$accountsneeded = 1000

for($userid = 1; $userid -le $accountsneeded ; $userid++) 
{ 
    $managerid = [int]($userid/10) 
    if($managerid -gt 0) 
    { 
        Write-host "User $userid reports to userid $managerid" 
        New-ADUser -SamAccountName "$username$userid" `
                    –Path $ADContainer .\Contacts `
                    -GivenName "$username$userid" `
                    -Surname $userid `
                    -Name "$username$userid" `
                    -DisplayName "$username$userid" `
                    -Manager "$username$managerid" `
                    -AccountExpirationDate $null `
                    -AccountPassword $secure `
                    -ChangePasswordAtLogon $False `
                    -Enabled $True `
                    -PasswordNeverExpires $True 
        Write-host "User $username$userid created" 
    } 
    else 
    { 
        New-ADUser -SamAccountName "$username$userid" `
                    -Path $ADContainer `
                    -GivenName "$username$userid" `
                    -Surname $userid `
                    -Name "$username$userid" `
                    -DisplayName "$username$userid" `
                    -AccountExpirationDate $null `
                    -AccountPassword $secure `
                    -ChangePasswordAtLogon $False `
                    -Enabled $True `
                    -PasswordNeverExpires $True 
        Write-host "User $username$userid created" 
    } 
}

This script will create a number of test accounts up to $accountsneeded (defaulted to 1000). The $adcontainer is where you would like to create the accounts. The script creates a reporting structure where each user has ten direct reports. This should build out a nice test org structure so you can see how colleagues and the org chart works in SharePoint. There are many other attributes you can set on the ADUser, so add additional parameters to the New-ADUser commandlet as needed.

Happy Scripting!

Technorati Tags: Powershell,SharePoint,Test Accounts,SharePoint Farm Setup,New-ADUser,Active Directory