I've been dabbling with Powershell to try to get a handle on it recently. I've done some Perl scripting in the past but that was over 10 years ago so my scripting skills have waned! I've been working on a project with practical implications for a job I'm doing since that tends to help things stick better. This project is to write a Powershell script that can add users from a CSV file to Active Directory and add them to the correct security group.
I'm running into an issue where users with hyphenated last names cause the script to fail. My assumption is that this is because Powershell is interpreting the hyphen as a switch. My script is as follows:
Any help finding a way to get Powershell to allow me to use users with hyphenated last names would be very much appreciated.
I'm running into an issue where users with hyphenated last names cause the script to fail. My assumption is that this is because Powershell is interpreting the hyphen as a switch. My script is as follows:
Code:
Import-Csv .\userlist.csv | foreach-object {
$userprinicpalname = $_.SamAccountName + "@test.MYDOMAINNAME.net";
New-ADUser -SamAccountName $_.SamAccountName -UserPrincipalName $userprinicpalname -Name $_.name -DisplayName $_.name -GivenName $_.cn -SurName $_.sn -Title $_.Title -Department $_.Department -Path $_.path;
Set-ADAccountPassword -identity $_.SamAccountName -NewPassword (ConvertTo-SecureString -AsPlainText $_.Password -Force) -Reset ;
Enable-ADAccount -identity $_.SamAccountName;
Set-ADUser -Identity $_.SamAccountName -ChangePasswordAtLogon $true;
Add-ADGroupMember -Identity $_.GroupName -Member $_.SamAccountName
}