Whether you’re writing quick one-liners or building full automation scripts in PowerShell, variables are a cornerstone concept. They allow you to store, reuse, and manipulate data in flexible and powerful ways. In this guide, we’ll explore everything from basic usage to practical tips that will help both beginners and those looking to sharpen their scripting skills.
What Is a Variable in PowerShell?
A variable is simply a storage location that holds a value. In PowerShell, all variables start with a dollar sign ($
). You can assign values to variables without declaring a type:
1 2 3 |
$name = "Azure365Addict" $count = 10 $price = 29.99 |
Variables are dynamically typed, meaning you can store any kind of data in them: strings, integers, arrays, objects, and more.
Declaring and Using Variables
Declaring a variable is straightforward:
1 2 |
$greeting = "Hello, PowerShell!" echo $greeting |
You don’t need to initialize variables before using them, but uninitialized variables will return $null
.
Variable Naming Conventions
PowerShell is flexible with variable names, but here are some good practices:
- Use camelCase or PascalCase:
$userName
,$TotalCount
- Be descriptive: avoid names like
$x
or$temp1
- Avoid reserved keywords like
$if
,$true
,$foreach
Common Variable Types
PowerShell variables can hold many types of data. Here are a few common examples:
Strings
1 |
$message = "Welcome to PowerShell" |
Integers
1 |
$counter = 42 |
Arrays
1 |
$colors = @("Red", "Green", "Blue") |
Hashtables
1 |
$user = @{ Name = "John"; Age = 30; Role = "Admin" } |
Objects
1 2 |
$process = Get-Process | Select-Object -First 1 $process.ProcessName |
You can inspect any variable’s type like this:
1 |
$colors.GetType().Name |
Working with Arrays and Hashtables
PowerShell makes it easy to work with collections:
Arrays
1 2 3 4 |
$items = @("One", "Two", "Three") foreach ($item in $items) { Write-Host $item } |
Hashtables
1 2 |
$person = @{FirstName = "Jane"; LastName = "Doe"} Write-Host "Full name: $($person.FirstName) $($person.LastName)" |
Best Practices and Pitfalls
- Use meaningful names – Helps with readability and debugging
- Avoid hardcoding values – Use variables to make scripts more flexible
- Use quotes carefully –
$name = "User"
vs'User'
, especially when interpolating variables in strings - Check for null values – Prevent errors with conditional checks:
if ($var -ne $null)
Real-World Example: User Info Script
Here’s a practical script that uses variables to display system information:
1 2 3 4 5 6 7 8 9 10 |
$computer = $env:COMPUTERNAME $user = $env:USERNAME $uptime = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime $ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notlike "Loopback*" }).IPAddress Write-Host "System Information" Write-Host "-------------------" Write-Host "Computer Name: $computer" Write-Host "Logged-in User: $user" Write-Host "System Uptime: $($uptime.Days) days" Write-Host "IP Address: $ip" |
Conclusion
Variables in PowerShell are more than just a way to hold values – they’re a powerful part of your scripting toolbox. From tracking system info to building reusable scripts, understanding how variables work will elevate your scripting skills and make your automation more robust.