Posts

Showing posts from 2019

String Interpolation in Powershell Strings

I've been re-learning PowerShell lately. String Delimiters There are two string delimiters available for building strings in PowerShell: apostrophe (') and double quote ("): Write-Host 'Hello World' Write-Host "Hello World" Both of these PowerShell commands send the string Hello World to the console. I fell into the habit of using the apostrophe in my PowerShell scripts, and found that I missed string interpolation that I use so often in C# and PHP. Consider the following example: $name='Rick' $distanceInMiles=10 $greeting = 'Hello, my name is '+$name+' and I work '+$distanceInMiles+' miles from home.' Write-Host $greeting When this script is executed, the following is written to the console: Hello, my name is Rick and I work 10 miles from home. String Interpolation String interpolation lets you embed expressions into a string when defining the string. The expressions are evaluated when