Monday, August 20, 2018

Powershell - create uses from CSV in Active Directory

I did not write this, but fixed it, but I know it will come in handy again.



$targetpassword=ConvertTo-SecureString "Password!" -asplaintext -force
$thisUser= New-Object System.Management.Automation.PSCredential ("name\service_account", $targetpassword)
#$thisUser=Get-Credential

cls
$csv = Import-Csv -Path "C:\temp\User-create.csv"

foreach ($i in $csv) {
#$i.SamAccountName
$pass = [string] $i.password
$oupath = [string] $i.OUDN

New-ADUser -SamAccountName $i.samaccountname -Name $i.Name -GivenName $i.givenname -Surname $i.surname -DisplayName $i.name -UserPrincipalName $i.upn -EmailAddress $i.emailaddress -Path $oupath -Department $i.AgencyNameinCRM -OfficePhone $i.WorkPhoneNumber -title $i.Jobtitle -mobile $i.mobile -streetAddress $i.Street -city $i.city -postalCode $i.PostalCode -description $i.Description  -Credential $thisUser

#write-host -SamAccountName $i.samaccountname -Name $i.Name -GivenName $i.givenname -Surname $i.surname -DisplayName $i.name -UserPrincipalName $i.upn -EmailAddress $i.emailaddress -Path $oupath -Department $i.AgencyNameinCRM -OfficePhone $i.WorkPhoneNumber
write-host "Creating"  $i.givenname $i.surname

Set-ADAccountPassword -Identity $i.SamAccountName -NewPassword (ConvertTo-SecureString -AsPlainText $pass -Force) -Reset  -Credential $thisUser
Set-ADUser -Identity $i.SamAccountName -Enabled $true  -Credential $thisUser
}

No comments:

Blog Archive