0

How do i prevent the "Employee" text becoming part of the $number variable and get the string to work. It's a non powershell command I'm trying to run in a Powershell script.

$firstname = "John"
$Lastname = "Smith"
$Number = "7"

gam update group "Office$NumberEmployee" add member user "[email protected]"

3 Answers 3

1

Use braces to demarcate boundaries of a variable within a powershell script. Example below:

  gam update group "Office${Number}Employee" add member user "${Firstname}.${Lastname}@email.com"
Sign up to request clarification or add additional context in comments.

1 Comment

I tried that but it seems to remove the variable. I got this error gam : Group: [email protected], Does not exist
0

You can use single quotes or the escape character in PowerShell to prevent it from trying to expand the string following your $ (dollar sign).

The most common is to just use single quotes for the string...but it is not really best practice to use one over the other that I'm aware of, purely preference. I think as long as you use one method among all your scripts is sufficient.

Examples (with screenshot):

"Office$NumberEmployee"
'Office$NumberEmployee'
"Office`$NumberEmployee"

enter image description here

Comments

0

I found a workaround by just creating the variable.

$firstname = "John"
$Lastname = "Smith"
$Number = "7"
$NumberEmployee = $Number + "Employee"

gam update group "Office$NumberEmployee" add member user "[email protected]"

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.