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!

Anonymous
  • Just spit-ball'in here:

    Here are few more ways to do this. As the prblem here is not dealing the *** in this string, since there is no variable called
    $env:windir:

    Using backtick
    "$env:windir`: this is my Windows folder"
    C:\WINDOWS: this is my Windows folder

    Using a space
    "$env:windir : this is my Windows folder"
    C:\WINDOWS : this is my Windows folder

    Using parens
    "$($env:windir): this is my Windows folder"
    C:\WINDOWS: this is my Windows folder

    You could also do these. Though I am not real sure why one would, as it would now need a -replace to remove those characters,  and that's just extra work and unecessary. But just say'in...

    "$env:windir): this is my Windows folder"
    C:\WINDOWS): this is my Windows folder

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

     "$env:windir#: this is my Windows folder"
    C:\WINDOWS#: this is my Windows folder

     "$env:windir|: this is my Windows folder"
    C:\WINDOWS|: this is my Windows folder

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

     "$env:windir/: this is my Windows folder"
    C:\WINDOWS/: this is my Windows folder

     "$env:windir]: this is my Windows folder"
    C:\WINDOWS]: this is my Windows folder