0
New-ADUser -SamAccountName $user.SamAccountName -Name ($user.FirstName + " " + $user.LastName) `
    -DisplayName ($user.FirstName + " " + $user.LastName) -GivenName $user.FirstName -Surname $user.LastName `
    -EmailAddress ($user.FirstName + "_" + $user.LastName + $dnsroot) -UserPrincipalName ($user.SamAccountName + $dnsroot) `
    -Title $user.title -manager $user.manager `
    -Enabled $true -ChangePasswordAtLogon $false -PasswordNeverExpires  $true `
    -AccountPassword $defpassword -PassThru `
    -AccountExpirationDate $expires -Path 'rop.com/ts/otos/ate/PMO/'` 
    -telephoneNumber "9856"'
    -LoginScript "es.cmd"'
    -Description "etant"'
    -Street "unt"`

I don't work with PowerShell much so I am unsure how to fix this error.

The error I get is: Missing expression after unary operator '-'

2 Answers 2

1

There are a few issues.

As mentioned, you need to have a space between your backticks and the end of a line for line continuation. Also, on some of your last lines, you use single quotes (') instead of backticks (`).

If your last line in the sample code is the last line of your command, having a backtick at the end of it will cause errors.

Additionally, -telephoneNumber is not a parameter of New-ADUser. The only default parameters that deal with phone numbers are -HomePhone, -OfficePhone, and -MobilePhone. Otherwise, you need to use the -OtherAttributes parameter.

In this case I think you want -OtherAttributes @{telephonenumber="9856"}

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for this because I was getting an error due to telephoneNumber not being a parameter
1

I'm pretty sure you need a space before the backtick.

New-ADUser -SamAccountName $user.SamAccountName -Name ($user.FirstName + " " + $user.LastName) `
-DisplayName ($user.FirstName + " " + $user.LastName) -GivenName $user.FirstName -Surname $user.LastName `
-EmailAddress ($user.FirstName + "_" + $user.LastName + $dnsroot) -UserPrincipalName ($user.SamAccountName + $dnsroot) `
-Title $user.title -manager $user.manager `
-Enabled $true -ChangePasswordAtLogon $false -PasswordNeverExpires  $true `
-AccountPassword $defpassword -PassThru `
-AccountExpirationDate $expires -Path 'rop.com/ts/otos/ate/PMO/' ` 
-telephoneNumber "9856" `
-LoginScript "es.cmd" `
-Description "etant" `
-Street "unt"

5 Comments

that doesn't seem to be the problem because I still get that error
Did you copy/paste my example code? I also fixed the apostrophes which should have been backticks.
it worked now. Any idea how the apostrophe and back tick conventions work? This is my first time writing powershell for AD so I am unsure of the syntax.
not sure if you will get my message but now I get the error: Could not create user , A parameter cannot be found that matches parameter name 'telephoneNumber'.
The backtick is the "line continuation" character. It basically means that you can span a single command across multiple lines. As for the -TelephoneNumber parameter, that does not exist on the New-ADUser command. You will need to specify the telephone number attribute using the -OtherAttributes parameter. Use Get-Help New-ADUser -Param OtherAt* for more information about the parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.