Monthly Archive

Categories

Working with strings: I’m a substitute

PowerShell string substitution is a very neat trick

PS (1) > $string = "World"
PS (2) > "Hello $string"
Hello World

 

We can substitute the value of the variable into the string. The catch – it only works with double quoted strings i.e. we use “”

If we use single quotes ‘’

PS (3) > 'Hello $string'
Hello $string

 

we don’t get the substitution. One point to not though is that in this case

PS (4) > "Hello '$string'"
Hello 'World'

 

we do get substitution because the single strings are classed as part of the overall string.

Simple rule – when you need to substitute values into a string use double quotes

Leave a Reply