Reputation: 29
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
Upvotes: 1
Views: 245
Reputation: 81
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"
Upvotes: 0
Reputation: 58931
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
Upvotes: 3