In PowerShell, when you need to show a variable's value in a string you typically just add the variable along with some other text inside of a string with double quotes.

This is perfectly fine if $serverName
has a value. But what if this is part of a larger script and you have various conditions that might set $serverName
. This means that $serverName
might not contain any value at all. At that point, your message just looks messed up.

The solution? Make it a habit of enclosing your variables inside of strings with simple brackets.

You can now clearly see that a variable should have been there but was not.
It's a simple trick but it's saved me so much troubleshooting time over the years. Now, it's habit to enclose variables in brackets. In fact, I have a snippet that I use to simply type varb
and it will automatically do it for me.
Join the Jar Tippers on Patreon
It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!
Become a Patron!Subscribe to Adam the Automator
Get the latest posts delivered right to your inbox
Comments powered by Talkyard.