I created the following PowerShell script that works to create a new AD user and respective attributes, as long as I update the script with the user's name every time I want to run it. Here is what I have:
Import-Module ActiveDirectory
New-ADUser -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "John" -DisplayName "John Doe" -UserPrincipalName "[email protected]" -Country "US" -EmailAddress [email protected] -Path "OU=email1.com,OU=User Accounts,DC=ADDomain,DC=com" -AccountPassword(Read-Host -AsSecureString "Input Password") -Enabled $true
Set-ADUser -Identity John -Add @{Proxyaddresses="SMTP:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]"}
Add-ADGroupMember -Identity Office365Users -Members John
I would like to do something with prompts to set variables regarding the name. I have tried doing this, but can't get the combined variables with a period in between working properly for the email addresses, the space in between the first and the last name for name/display name and possibly adding them to the the various domains, both UPN and email. Here is what I have so far.
Import-Module ActiveDirectory
$GivenName = Read-Host -Prompt "Enter First Name"
$Surname = Read-Host -Prompt "Enter Last Name"
$SecurePW = Read-Host -Prompt "Enter a Password" -AsSecureString
New-Variable -Name $DisplayName = "${GivenName}${Surname}"
New-ADUser -Name "$DisplayName" `
-AccountPassword $SecurePW `
-SamAccountName $GivenName `
-DisplayName $DisplayName `
-PasswordNeverExpires $True `
-UserPrincipalName $GivenName + "@UPNDomain.com" `
-Country US `
-EmailAddress $GivenName + "." + $Surname + "@email1.com" `
-Path "OU=email1.com,OU=User Accounts,DC=upn,DC=com" `
-Enabled $True
Set-ADUser -Identity $GivenName -Add @{Proxyaddresses="SMTP:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]"}
Add-ADGroupMember -Identity Office365Users -Members $GivenName
I get an error starting at New-ADUser : A positional parameter cannot be found that accepts argument '+'. on line 6 char1. I have messed around with syntax several times, so it may or may not be "complete", but I do know that it is wrong LOL