---
title: Place PowerShell Variables in Strings: Quick Tips
description: Learn to handle PowerShell variables in strings effectively. Account for null values like a pro.
canonical: https://adamtheautomator.com/powershell-variable-in-strings/
---

# Place PowerShell Variables in Strings: Quick Tips

> Learn to handle PowerShell variables in strings effectively. Account for null values like a pro.

Source: https://adamtheautomator.com/powershell-variable-in-strings/

---

- Place PowerShell Variables in Strings: Quick Tips Tap to hide Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

                Search for:

-

-

-

-

# Place PowerShell Variables in Strings: Quick Tips

        Published:1 July 2019 -

- PowerShell

          [](https://adamtheautomator.com/author/adam-bertram/)

            [Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)

            Read [more tutorials](https://adamtheautomator.com/author/adam-bertram/) by Adam Bertram!

-

-

        [](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=display)
        Audit Active Directory for stale users, weak passwords, and other security risks with [Specops Password Auditor](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=text).

                XFacebookLinkedIn

When you need to show a PowerShell variable in a string, you typically just add the variable along with some other text inside of a string with double-quotes as shown below

PowerShell variable in string

Related: [Back to Basics: PowerShell Strings](https://adamtheautomator.com/powershell-strings/)

Putting a variable inside of a double-quoted string is called [variable expansion](https://devblogs.microsoft.com/powershell/variable-expansion-in-strings-and-here-strings/). This method of variable insertion is fine if the variable in question has a value.

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

You can see below if you just insert the PowerShell variable inside of the string and then return the value, nothing is shown. You have no way to know if that value even contained a variable reference at all!

No variable expansion in verbose message

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

Easy inspection of no variable value

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.

  Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

  [Explore ATA Guidebooks](https://adamtheautomator.com/ata-guidebooks/)

## More from ATA Learning and Partners

- ### Recommended Resources! Recommended Resources for Training, Information Security, Automation, and more!

- ### Get Paid to Write! ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

- ### ATA Learning Guidebooks ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

## Categories

- IT Ops

- Cloud

- DevOps

- Home Ops

- Information Security

- Software Development

## Site

- Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

        Copyright 2026&copy; ATA Learning | [Privacy Policy](https://adamtheautomator.com/privacy/)

                                                        Don't be left behind with the ATA Learning Newsletter!

Looks like you're offline!
