DEV Community

Moon Light
Moon Light

Posted on

PowerShell Scripts Every MSP Should Use

If you work in an MSP environment, you already know the truth:

๐Ÿ‘‰ Repetitive tasks will slowly destroy your soul.

  • Checking disk space
  • Monitoring services
  • Logging into multiple machines
  • Fixing the same issues again and again

At some point, you realize:

โ€œIโ€™m not being paid to click the same thing 50 times.โ€

Thatโ€™s where PowerShell saves you.

Butโ€ฆ

After a few โ€œlearning experiencesโ€ (a.k.a. breaking things in production ๐Ÿ˜…), I realized:

๐Ÿ‘‰ Automation is powerfulโ€ฆ but dangerous.

So here are 5 PowerShell scripts every MSP should useโ€”written in a safe, production-friendly way.


๐Ÿ› ๏ธ 1. Disk Space Check (Prevent Tickets Before They Happen)

This is one of the most common MSP issues.

Instead of waiting for:

โ€œMy computer is slowโ€

You can detect problems early.

```powershell id="disk01"
$computers = @("PC1","PC2","PC3")

foreach ($computer in $computers) {
try {
$disks = Get-CimInstance Win32_LogicalDisk -ComputerName $computer -Filter "DriveType=3"

    foreach ($disk in $disks) {
        $freePercent = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 2)

        if ($freePercent -lt 20) {
            Write-Output "$computer - Low disk space on $($disk.DeviceID): $freePercent%"
        }
    }
}
catch {
    Write-Output "Failed to check $computer"
}
Enter fullscreen mode Exit fullscreen mode

}




๐Ÿ‘‰ **Why this matters:**
You fix issues *before* users notice.

---

# ๐Ÿ”„ 2. Safe Restart Script (No More Accidental Chaos)

Letโ€™s be honestโ€ฆ

๐Ÿ‘‰ Weโ€™ve all restarted the wrong machine at least once.

Hereโ€™s a safer version:



```powershell id="restart01"
$computers = @("PC1","PC2")

foreach ($computer in $computers) {
    if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
        Restart-Computer -ComputerName $computer -WhatIf
    } else {
        Write-Output "$computer is offline"
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Key feature: -WhatIf
This shows what would happen without actually doing it.


๐Ÿง  3. Service Health Check (Catch Hidden Problems)

Some services fail silently.

This script helps you detect them quickly:

```powershell id="service01"
$services = @("Spooler","W32Time")

foreach ($service in $services) {
$status = Get-Service -Name $service

if ($status.Status -ne "Running") {
    Write-Output "$service is NOT running"
}
Enter fullscreen mode Exit fullscreen mode

}




๐Ÿ‘‰ **Upgrade idea:** Automatically restart failed services (carefully!)

---

# ๐Ÿ” 4. Local Admin Audit (Security Must-Have)

This is huge for cybersecurity.

You should always know:

๐Ÿ‘‰ **Who has admin access?**



```powershell id="admin01"
$admins = Get-LocalGroupMember -Group "Administrators"

foreach ($admin in $admins) {
    Write-Output $admin.Name
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Why this matters:
Unauthorized access = major security risk


๐Ÿ“‹ 5. Script Logging (The Thing Everyone Skips ๐Ÿ˜…)

This is not optional.

Seriously.

```powershell id="log01"
Start-Transcript -Path "C:\Logs\msp_script.log"

Write-Output "Script started..."

Your script here

Write-Output "Script completed."

Stop-Transcript




๐Ÿ‘‰ **Why this matters:**

* Debug faster
* Track actions
* Prove what happened

---

# โš ๏ธ The Most Important Rule

All these scripts are useful.

But the real difference is:

๐Ÿ‘‰ **How you run them**

---

## ๐Ÿง  My Safety Checklist (Always Follow This)

Before running any script:

* Use `-WhatIf` whenever possible
* Test on ONE machine first
* Check permissions
* Add logging
* Assume something will fail

---

# ๐Ÿ˜‚ Real Talk

PowerShell doesnโ€™t make mistakes.

๐Ÿ‘‰ **We do.**

PowerShell just executes our bad ideasโ€ฆ very efficiently.

---

# ๐Ÿš€ Final Thought

If you use these scripts correctly:

* You save hours
* You reduce errors
* You look like a hero

If you use them incorrectly:

๐Ÿ‘‰ You create a story for your next blog post ๐Ÿ˜…

---

# ๐Ÿ‘‡ Your turn

* Whatโ€™s your go-to PowerShell script?
* Ever automated something that wentโ€ฆ very wrong?

Letโ€™s share ๐Ÿ‘‡
Enter fullscreen mode Exit fullscreen mode

Top comments (0)