Separating Variables in Expandable Strings

by Aug 9, 2018

When you use double-quoted strings, you can expand variables inside of them like this:

 
PS C:\> "Windir: $env:windir"
Windir: C:\Windows 
 

However, there is no obvious way to mark the beginning and end of variables, so this will fail:

 
PS C:\> "$env:windir: this is my Windows folder"
 this is my Windows folder 

The solution is to use braces to identify the start end end of variables inside strings:

 
PS C:\> "${env:windir}: this is my Windows folder"
C:\Windows: this is my Windows folder
 

Twitter This Tip! ReTweet this Tip!