1

I am trying to run a PowerShell script using the information contained within a .csv file.

My script is below:

Import-CSV \\chem-fp01\shared areas\IT\New IT Folder (Do Not Delete \\Powershell Scripts\Create New User.csv | ForEach-Object {
  $Password = ConvertTo-SecureString $_.password -AsPlainText -Force

  New-Mailbox -Name $_.Name
  -FirstName $_.FirstName
  -LastName $_.LastName
  -Alias $_.Alias
  -UserPrincipalName $_.UserPrincipalName
  -Password $password
  -ResetPasswordOnNextLogon $false 
}

I am getting the error below and don't know what it means

Missing expression after unary operator '-'.
At C:\Scripts\CreateNewUser.psl:7 char:3
+ - <<<<LastName $_.LastName
    + CategoryInfo          : ParserError: (-:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

2 Answers 2

3

You have to either, write all parameters to one line:

New-Mailbox -Name $_.Name -FirstName $_.FirstName -LastName $_.LastName -Alias $_.Alias -UserPrincipalName $_.UserPrincipalName -Password $password -ResetPasswordOnNextLogon $false 

Or you can use splatting (Thanks Frode F.):

$parameters = @{
    Name = $_.Name 
    FirstName = $_.FirstName 
    LastName = $_.LastName 
    Alias = $_.Alias 
    UserPrincipalName = $_.UserPrincipalName 
    Password = $password 
    ResetPasswordOnNextLogon = $false 
}

New-Mailbox @parameters

Another solution would be to use the ` character at the end of the line (not recommended) :

New-Mailbox -Name $_.Name `
-FirstName $_.FirstName `
-LastName $_.LastName `
-Alias $_.Alias `
-UserPrincipalName $_.UserPrincipalName `
-Password $password `
-ResetPasswordOnNextLogon $false 
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you...what do you mean by character at the end of the line?
If your cmdlet invocation spawns multiple lines, you have to tell PowerShell using the ` symbol, that the next line belongs to the line above.
Thank you. I have put the ` at the end of each line as you said. However, now I am getting the following error. Missing closing > in statement block. At C:\scripts\createNewUser.ps1:15 char:1 *<<<< *CategoryInfo : ParserError: <CloseBraceToken:TokenId> [], ParseException *FullyQualifiedErrorID: MissingEndCurlyBrace
As the error message tells you, you are missing the } at the end. I will just edit my answer so you can copy paste it...
Please do not recommend using backticks. Backticks are evil. Use splatting or the normal one-line way.
|
0

The reason this command fails, is because your CSV file path contains spaces, and you are not surrounding it with double or single quotes.

Whenever your path contains blank spaces, just add quotes at the beginning and end of the path like this:

Import-CSV "\\chem-fp01\shared areas\IT\New IT Folder (Do Not Delete \\Powershell Scripts\Create New User.csv"

Comments

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.