Skip to content

PowerShell Best Practices for MSPs: Writing Scripts Like a Pro (Without Losing Your Sanity)

For Managed Service Providers (MSPs), PowerShell is the Swiss Army knife of IT tools. It’s versatile, powerful, and a little dangerous if not used correctly—kind of like giving a toddler a chainsaw. Writing scripts that are clean, efficient, and safe is essential, especially when a single line of code can turn a routine task into a fire drill. Let’s dive into some PowerShell best practices for MSPs, with a touch of humor and a focus on practicality. Because while scripting is serious business, it doesn’t have to be boring.

1. Commenting Like a Human, Not a Robot

Bad Practice:

# This line sets the variable $x to 10
$x = 10

# This line adds $x to $y and stores the result in $z
$z = $x + $y

Good Practice:

# Set initial count of widgets. Start with 10.
$x = 10

# Calculate total cost based on widget count and price per widget.
$z = $x * $pricePerWidget

Explanation:
Let’s be honest: overly detailed comments are about as useful as a chocolate teapot. You don’t need to narrate every single line of code like it’s a play-by-play. Focus on the why instead of the what. Why are you setting this variable? Why are you performing this calculation? Clear, concise comments help your fellow techs understand your logic without wading through a sea of redundant explanations. Plus, they’ll thank you for not writing a novel.

2. Use Proper Naming Conventions (Because x Could Be Anything)

Bad Practice:

$x = "John"
$y = "Doe"
$z = $x + " " + $y

Good Practice:

$FirstName = "John"
$LastName = "Doe"
$FullName = "$FirstName $LastName"

Explanation:
Variables like $x, $y, and $z are fine if you’re writing code for a math exam. But in the real world of Managed Service Providers, vague names are a recipe for disaster. Use clear, descriptive variable names that explain exactly what they’re for. This makes your script easier to read and maintain. And when you’re deep in troubleshooting mode at 2 AM, you’ll appreciate not having to play a guessing game with your own code.

3. Handle Errors Gracefully, Don’t Just Hope for the Best

Bad Practice:

Get-Item "C:\SomeNonExistentFile.txt"

Good Practice:

try {
    Get-Item "C:\SomeNonExistentFile.txt"
} catch {
    Write-Host "The file does not exist. Please check the file path."
}

Explanation:
Scripts that fail without explanation are like horror movies without a plot—just a series of jump scares that nobody enjoys. Instead of letting your script blow up in your face, use error handling to catch problems and provide meaningful feedback. This makes your scripts robust and user-friendly, turning a potential nightmare into a manageable hiccup. Remember, hope is not a strategy—especially not in IT.

4. Test Your Scripts in a Safe Environment (a.k.a. Don’t Be a Cowboy)

Bad Practice:

# Running scripts directly on production
Restart-Computer -ComputerName "ClientMachine"

Good Practice:

# Test your script in a development environment first
if ($env:COMPUTERNAME -eq "DevMachine") {
    Restart-Computer -ComputerName "DevMachine"
} else {
    Write-Host "This script should not be run on a production machine."
}

Explanation:
Scripting directly in a production environment is like performing open-heart surgery on yourself. It’s risky, messy, and rarely ends well. Always test your scripts in a controlled, non-production environment. And for goodness’ sake, label your test scripts clearly to prevent them from sneaking into live environments. Your future self—and your clients—will thank you.

5. Peer Review: It’s Not Just for Book Clubs

Even the best scriptwriters make mistakes (yes, even you). That’s why peer reviews are essential. They catch errors, improve code quality, and provide different perspectives on potential issues. Think of it like having a co-pilot double-check your work before takeoff. You wouldn’t want to fly solo without making sure everything’s in order, right? The same goes for scripts in an MSP environment.

UPCOMING DECEMBER WEBINAR ON AUTOTASK KANBAN

In this webinar, Dustin Puryear, Autotask expert and MSP industry veteran, will show you how to set up Kanban boards in Autotask, integrate them with your workflow rules, and how to get the most out of them.

Share via
Copy link
Powered by Social Snap