PowerShell Variables Tutorial: A Beginner’s Guide

Published:11 November 2024 - 2 min. read

Think of PowerShell variables as labeled storage containers – they hold whatever information you need to access later in your scripts. Just like how you might label boxes when moving house, variables help you organize and track data in your PowerShell scripts. In this tutorial, you will learn how to effectively work with these “storage containers” in PowerShell.

Prerequisites

To follow along with the examples in this tutorial, please be sure you have:

Creating Your First Variable

Picture this: you’re writing a script to process files in a specific folder, and you’ll need to reference that folder path multiple times. Instead of typing out the full path each time (and risking typos!), you can store it in a variable.

Variables in PowerShell always start with a dollar sign ($) followed by the name. Here’s how you’d store that folder path:

$processingFolder = 'C:\ImportantFiles\ToProcess'

Now anytime you need that path, just use $processingFolder. It’s like creating a shortcut to that longer piece of text.

The Perils of Undefined Variables

But what happens if you try to use a variable you haven’t created yet? By default, PowerShell is pretty forgiving – it’ll just return nothing. However, when writing scripts, that silent failure might mask problems. Let’s make PowerShell more strict with Set-StrictMode.

Set-StrictMode -Version Latest
$undefinedVariable

Now PowerShell will let you know right away if you’re trying to use a variable that doesn’t exist – much better for catching mistakes early!

The PowerShell Way: Using Variable Cmdlets

While the basic $variable = value syntax works fine, PowerShell provides specialized cmdlets that give you more control and visibility. Think of these as the “professional-grade” tools for working with variables.

Set-Variable: The Power Tool for Variable Creation

Instead of the basic assignment, you can use Set-Variable:

Set-Variable -Name logFile -Value 'C:\Logs\ProcessingLog.txt'

This approach gives you more options, like making variables read-only or hiding them from view.

Get-Variable: X-Ray Vision for Your Variables

Need to inspect your variables more closely? Get-Variable is your friend:

Get-Variable -Name logFile

You get back not just the value, but a proper PowerShell object with additional properties – super helpful when building robust scripts.

Real-World Example: Log File Processing

Let’s put this knowledge to work in a practical scenario. Say you’re writing a script to process log files:

# Store our config in variables
$logPath = 'C:\Logs'
$archivePath = 'C:\Logs\Archive'
$maxAgeDays = 30

# Now we can easily reference these throughout our script
Get-ChildItem -Path $logPath -Filter *.log |
    Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$maxAgeDays) } |
    Move-Item -Destination $archivePath

This is where variables really shine – the script is more readable and easier to modify. Need to change the max age? Just update one variable at the top.

Tips for Variable Success

Here are some battle-tested tips from the trenches:

  1. Use descriptive names – $userList is better than $ul
  2. Enable strict mode during development – catch problems early
  3. Use Set-Variable when you need extra control
  4. Remember, variables don’t survive between sessions
  5. Consider using a PowerShell profile for variables you always need

Next Steps

Now that you’ve got the basics down, you’re ready to explore more advanced variable concepts like:

  • Arrays and hash tables for storing collections
  • Custom objects for complex data
  • Variable scope (think local vs. global)
  • Automatic variables that PowerShell maintains

Conclusion

Variables are like the workbench of PowerShell scripting – they give you a place to store and organize all the pieces you’re working with. Whether you’re writing a quick one-liner or building complex automation, solid variable management will make your PowerShell journey much smoother.

Want to dive deeper? Check out PowerShell’s built-in help with Get-Help about_Variables. And remember, practice makes perfect – try using these concepts in your own scripts to really make them stick!

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

Explore ATA Guidebooks

Looks like you're offline!