Saturday, April 9, 2011

PowerShell: Create Users in active directory and Sharpoint 2010

Small powerschell script that creates an amount of users in the Active Directory and inserts them in the Sharepoint 2010

Import-Module ActiveDirectory
if((Get-PSSnapin -Name Microsoft.Sharepoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
 Add-PSSnapin Microsoft.Sharepoint.PowerShell
}
#Get-Command -Module ActiveDirectory
clear
$i =3
while ($i -le 100000) {
$j=$i.ToString()
$username="Dev$j"
$webUrl="http://sharepoint website url/"
$group="Development Owners"

New-ADUser -SamAccountName $username -Name $username -UserPrincipalName "$username@domain.com" -DisplayName "$username User" -GivenName $username -Surname "User" -AccountPassword (ConvertTo-SecureString -AsPlainText "pass@word1" -Force) -Enabled $true -PasswordNeverExpires $false -Path 'CN=Users,DC=Domain,DC=com'

New-SPUser -UserAlias "Domain\$username" -Web $webUrl -DisplayName "$username User" -Group $group

Set-SPUser -Identity "Domain\$username" -Web $webUrl  -SyncFromAD:$true
$i=$i+1
}

No comments:

Post a Comment