PowerShell Strikes Back: The Empire of Variables5 min read

This is Week 2 of PowerShell Strikes Back – a four-week May series for SQL Server DBAs who have dabbled in PowerShell but never stopped to nail down the fundamentals. If you missed Week 1 on single vs double quotes, start there first – it’ll make this post land better.

Last week, we learned that quotes are not created equal. This week, we’re going deeper into the building blocks that make PowerShell scripts actually useful: variables.

If quotes are your lightsaber, variables are the Force itself. They carry information from one part of your script to another. They make the difference between a script that works on one server and a script that works on all of them. Get comfortable with variables, and you’ll look back at your pre-PowerShell DBA life the way Luke looked back at Tatooine – relieved to be moving on.


The Basics – Declaring and Assigning Variables

In PowerShell, every variable starts with a dollar sign ($). You don’t need to declare a variable before using it – you just assign it a value and PowerShell figures out the rest.


Notice there are no data type declarations here. PowerShell infers the type from what you assign, strings get quotes, integers don’t, and booleans use the built-in $true and $false values. For most DBA scripting, this works perfectly fine.


Why Loose Typing Can Strike Back

PowerShell’s automatic type inference is convenient until it isn’t. Consider this:


The Empire strikes back through your own assumptions. If you need a variable to behave as a specific type, declare it explicitly by putting the type in brackets before the variable name:


As a DBA, you’ll care about this most when dealing with port numbers, SPIDs, database IDs, and any value that comes back from a query as a string but needs to be treated as a number.


Variables That DBAs Actually Use

Let’s get practical. Here are the kinds of variables you’ll build real scripts around.

Server and Instance Lists

Credentials

Date and Time Variables

File Paths


 


Storing Query Results in Variables

This is where variables go from useful to genuinely powerful for DBA work. You can store the results of a SQL query or a dbatools command directly in a variable and work with the data in PowerShell.




Variable Scope — Know Your Territory

Variables in PowerShell have a scope; they exist in a specific context and may not be visible outside it. For most DBA scripts running top to bottom in a single file, this won’t trip you up. But once you start writing functions or calling scripts from other scripts, scope matters.


The Jedi rule of thumb: default local scope is fine for most scripts. Only reach for $script: or $global: When you have a specific reason, unnecessary global variables are the midi-chlorians of PowerShell. Everyone argues about them, and they usually cause more problems than they solve.


A Few Variables You Get for Free

PowerShell has a set of automatic variables that are always available and genuinely useful for DBA scripting:

Variable What It Contains DBA Use Case
$_ or $PSItem Current pipeline object Used inside loops and Where-Object filters
$true / $false Boolean values Flag variables, conditional logic
$null Empty/no value Checking if a result came back empty
$Error An array of recent errors Checking what went wrong after a failure
$MyInvocation Info about the current script Getting the script name for log entries
$PSScriptRoot The directory the script lives in Building relative paths from the script location

Your Assignment, Rebel Pilot

  1. Open a PowerShell window and create typed variables for a server name (string), a port number (int), and a backup date (datetime).
  2. Build a backup file path string using double quotes that combines all three into a meaningful path.
  3. If you have dbatools available, run Get-DbaDatabase against a local or dev instance and store the results in a variable. Then check $Results.Count to see how many databases came back.

Variables are the backbone of every script you’ll write. Get comfortable assigning, typing, and reading them. Next week, we put them to work inside loops.

Next week in PowerShell Strikes Back: Return of the Loop  foreach, ForEach-Object, and how to stop running the same command twelve times by hand. See you on May 18th.

2 Replies to “PowerShell Strikes Back: The Empire of Variables5 min read”

Leave a Reply

Your email address will not be published. Required fields are marked *