Wednesday, April 8, 2015

Create AD accounts from a csv in Powershell

I made this to create a bunch of users who need Apache website access, but don't actually need to log into domain, thus the PasswordNeverExpires, edit to your liking before using.

$domain = "ExampleDomain.com"
$pass = "ExamplePass-$@#%$#^"
Import-Csv \\adm-dc\redirect\kwalker\Desktop\Users.csv | ForEach-Object {  
 $first = $_.first  
 $last = $_.last  
 $email = $_.email  
 $location = $_.location  
 $name = $first + " " + $last   
 $sam = $first.Substring(0,2) + $last  
 $principal = $sam + "@" + $domain 
 Write-Host $name  
 New-ADUser -PasswordNeverExpires $true -Path "OU=POC,DC=EXAMPLEDOMAIN,DC=COM" -GivenName $first -Surname $last -AccountPassword (ConvertTo-SecureString $pass -AsPlainText -force) -DisplayName $name -Name $name -SamAccountName $sam -UserPrincipalName $principal  
 }  

No comments:

Post a Comment