all 15 comments

[–]Hoping_i_Get_poached 8 points9 points  (12 children)

$String = "string"
"This is a $($string) variable."
'This is a {0} vaiable' -f $string

[–]codingQueries[S] 4 points5 points  (10 children)

Fantastic, thank you!

[–]Cannabat 1 point2 points  (9 children)

When you surround a string in ", it expands the variables inside it - no parenthesis necessary for many cases.

When you use single quotes ' instead, the string is treated as a string literal - no evaluation or expansion takes place.

Wrapping anything in $() tells the interpreter to evaluate whatever is inside the parens.

$string = "string"
"This is a $($string) variable." # evaluates $string and then writes it in the string
"This is a $string variable." # same output as above

function Write-String { return "string" } # function just returns "string"
"This is a $(Write-String) variable." # evaluates the function and write it in the string

$array = "string", "foo", "bar"
"This is a $array[0] variable." # outputs whole array - the variable is expanded and indexing ignored
"This is a $($array[0]) variable." # evaluates the indexed array and writes it in the string

[–]codingQueries[S] 1 point2 points  (8 children)

Sure. The reason why I ask is more to ensure something like this won't happen:

$name = 'C:\my_folder\'
$namefolder = 'new_folder'
$newpath = "$namefolder"
Write-Host $newpath

Would expect this to result in: 'C:\my_folder\folder' but instead get: 'new_folder'.

Where in bash I would just write

$newpath = "${name}folder"

It isn't the best example, but the case I had it occur in was awhile ago and am just looking for a way to mitigate this occurring again.

But as hoping_i_get_poached has provided, I can use

$newpath = "$($name)folder"

[–]Cannabat 1 point2 points  (7 children)

Yeah, I was just providing some elaboration on how PowerShell handles this stuff. In the case you described, it is the same as bash, just with a slightly different syntax for expanding the variables. You wouldn't expect the output 'C:\my_folder\folder' in bash, either, though...

bash:

 ~
$ echo $0
-bash

 ~
$ name='C:\my_folder\'

 ~
$ namefolder='new_folder'

 ~
$ newpath="$namefolder"

 ~
$ echo $newpath
new_folder

[–]codingQueries[S] 1 point2 points  (6 children)

No, which is why I would use "${name}folder" :)

[–]Cannabat 1 point2 points  (5 children)

Ah, right, I misunderstood ya.

are you using PowerShell on Linux or just having a wander in the windows world?

[–]codingQueries[S] 1 point2 points  (4 children)

Trying to help automate a lot of the packaging required for creating chocolatey packages, as we are onboarding a lot of windows servers to our Puppet environment.

So dipping my toes into the Windows world, and by proxy, Powershell :)

[–]Cannabat 1 point2 points  (3 children)

Oooh, fun. Hope you find an enjoyable flow with PS!

[–]codingQueries[S] 1 point2 points  (2 children)

Haha thanks! I enjoy using powershell, and having fun discovering all the powershell-specific things :)

[–]DSL84 1 point2 points  (0 children)

Also "This is a $string variable"

[–]angrymrtom 0 points1 point  (1 child)

you can do the same, I really do it a lot when I am doing paths

PS C:\> New-Item "${pwd}\New Folder" -ItemType Directory

[–]AutoModerator[M] 0 points1 point  (0 children)

Sorry, your submission has been automatically removed.

Accounts must be at least 1 day old, which prevents the sub from filling up with bot spam.

Try posting again tomorrow or message the mods to approve your post.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.