Concat two strings in powershell and supply as one parameter

44,611

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.

Share:
44,611
BLang
Author by

BLang

Coding has been a hobby for sometime, and teaching/ tutoring is a passion! (Its so great when you see the light bulb go off in someone elses head!) This website perfectly combines both of my hobby with my passion, in such a clean and productive environment... usually... darn trolls!!

Updated on July 14, 2020

Comments

  • BLang
    BLang almost 4 years

    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 '+'."

  • Giorgi Chakhidze
    Giorgi Chakhidze almost 7 years
    Yet another one: put them inside parentheses -DisplayName ($firstName + " " + $lastName)