Jarrod
Jarrod

Reputation: 101

Why am I getting a "missing expression" error in my PS New-ADUser script?

The error I'm getting is "Missing expression after unary operator '-'" At line 63, char 14. So it's where the Path/OU is set, but I can't find anything wrong with it. Any help is appreciated. Thanks.

# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\ADMaint\NewUsers\NewUsers.csv
$Password = "Welcome01"
$OU = "ou=NewUsers,ou=Users,ou=Logins,dc=company,dc=com"

#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a     variable as below

$Firstname      = $User.firstname
$Middle         = $User.middle
$Lastname       = $User.lastname
$Department     = $User.department
$Title          = $User.title
$Office         = $User.office
$Address        = $User.address
$Company        = $User.company
$employeeNumber = $User.employeeNumber
$employeeID     = $User.employeeID
$Telephone      = $User.telephone
$Pager          = $User.pager
$Mobile         = $User.mobile
$Fax            = $User.fax
$Custom1        = $User.custom1
$Custom2        = $User.custom2
$Custom3        = $User.custom3
$Custom4        = $User.custom4
$DisplayName    = "$Lastname" + ", " + "$Firstname" + " " + "$Middle"
$Username       = "$lastname".ToLower() + "$firstname".substring(0,1).ToLower()

#Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
     #If user does exist, give a warning
     Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
    #User does not exist then proceed to create the new user account
    #Account will be created in the OU provided by the $OU variable read from the CSV file
    New-ADUser `
        -SamAccountName $Username `
        -UserPrincipalName "[email protected]" `
        -Name $DisplayName `
        -GivenName $Firstname `
        -surname $Lastname `
        -initials $Middle `
        -department $Department `
        -title $Title `
        -Office $Office `
        -streetAddress $Address `
        -Company $Company `
        -employeeNumber $EmployeeNumber `
        -employeeID $EmployeeID `
        -OfficePhone  $Telephone `
        -mobile $Mobile `
        -fax $Fax `
        -DisplayName $DisplayName`
        -Path $OU `
        -AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
        #-OtherAttribute @{pager="$(User."pager")"; extensionAttribute1="$(User."custom1")"; extensionAttribute2="$(User."custom2")"; extensionAttribute3="$(User."custom3")"; extensionAttribute4="$(User."custom4")"}  `
        -ChangePasswordAtLogon $true  `
        -Enabled $true `
}

}

Upvotes: 1

Views: 1252

Answers (1)

Nick Young
Nick Young

Reputation: 207

Can't verify now, but looks like there is a missing space before the ` on the previous line.

-DisplayName $DisplayName`

Multi-line commands require the space before the ` symbol.

Upvotes: 3

Related Questions