Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerTip: Include Expressions in a String in PowerShell

$
0
0

Summary: Learn how to include expressions in a string in Windows PowerShell.

Hey, Scripting Guy! Question How do I include expressions in a string in Windows PowerShell? They're not replaced correctly in a
          double-quoted string:

PS C:\> $p = Get-Process PowerShell
PS C:\>"The $p.Name process uses the $p.StartInfo.WindowStyle window style."

The System.Diagnostics.Process (powershell).Name process uses the
System.Diagnostics.Process (powershell).StartInfo.WindowStyle window style.

Hey, Scripting Guy! Answer Enclose the values in a new variable:

PS C:\>"The $($p.Name) process uses the $($p.StartInfo.WindowStyle) window style."
The powershell process uses the Normal window style.

         Or use a formatted string:

PS C:\>"The {0} process uses the {1} window style." -f $p.Name, $p.StartInfo.WindowStyle
The powershell process uses the Normal window style.


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>