3

I am trying to concatinate two strings into a variable and then supple the new variable as a parameter in a command

$firstName = Read-Host -Prompt 'enter user first name'
$lastName = Read-Host -Prompt 'enter user last name'
$userAblevetsEmail = '' + $firstName + '.' + $lastName + '@company.com'
New-MsolUser -UserPrincipalName $userAblevetsEmail  -DisplayName $firstName + " " + $lastName -FirstName $firstName

I get the following error:

"New-MsolUser : A positional parameter cannot be found that accepts argument '+'."

1
  • 2
    use the string formatter $userAblevetsEmail = "{0}.{1}@company.com" -f $firstName, $lastName Commented Jul 13, 2017 at 21:37

1 Answer 1

10

Your code New-MsolUser -UserPrincipalName $userAblevetsEmail -DisplayName $firstName + " " + $lastName -FirstName $firstName is slightly off.

The info following -DisplayName cannot have spaces without being contained inside of something.

Here is an example fix:

New-MsolUser -UserPrincipalName $userAblevetsEmail -DisplayName "$firstName $lastName" -FirstName $firstName

You can simply use the variables directly inside quotation marks.

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

1 Comment

Yet another one: put them inside parentheses -DisplayName ($firstName + " " + $lastName)

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.