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

This entry was posted in PowerShell V2, Strings. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s