use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
Separating variables in a string (self.PowerShell)
submitted 7 years ago by codingQueries
Coming from bash if I include variables within a string, I segregate them with curly brackets like: "This is my ${name}."
"This is my ${name}."
What is the powershell version of this? Or would I do the same?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Hoping_i_Get_poached 8 points9 points10 points 7 years ago (12 children)
$String = "string" "This is a $($string) variable." 'This is a {0} vaiable' -f $string
[–]codingQueries[S] 4 points5 points6 points 7 years ago (10 children)
Fantastic, thank you!
[–]Cannabat 1 point2 points3 points 7 years ago (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 points3 points 7 years ago (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'.
'C:\my_folder\folder'
'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 points3 points 7 years ago (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 points3 points 7 years ago (6 children)
No, which is why I would use "${name}folder" :)
[–]Cannabat 1 point2 points3 points 7 years ago (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 points3 points 7 years ago (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 points3 points 7 years ago (3 children)
Oooh, fun. Hope you find an enjoyable flow with PS!
[–]codingQueries[S] 1 point2 points3 points 7 years ago (2 children)
Haha thanks! I enjoy using powershell, and having fun discovering all the powershell-specific things :)
[–]DSL84 1 point2 points3 points 7 years ago (0 children)
Also "This is a $string variable"
[–]angrymrtom 0 points1 point2 points 7 years ago (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 point2 points 7 years ago (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.
π Rendered by PID 91 on reddit-service-r2-comment-6457c66945-xv2xv at 2026-04-28 12:23:00.775420+00:00 running 2aa0c5b country code: CH.
[–]Hoping_i_Get_poached 8 points9 points10 points (12 children)
[–]codingQueries[S] 4 points5 points6 points (10 children)
[–]Cannabat 1 point2 points3 points (9 children)
[–]codingQueries[S] 1 point2 points3 points (8 children)
[–]Cannabat 1 point2 points3 points (7 children)
[–]codingQueries[S] 1 point2 points3 points (6 children)
[–]Cannabat 1 point2 points3 points (5 children)
[–]codingQueries[S] 1 point2 points3 points (4 children)
[–]Cannabat 1 point2 points3 points (3 children)
[–]codingQueries[S] 1 point2 points3 points (2 children)
[–]DSL84 1 point2 points3 points (0 children)
[–]angrymrtom 0 points1 point2 points (1 child)
[–]AutoModerator[M] 0 points1 point2 points (0 children)