How to Write a PowerShell Script You Can Reuse

Start with a few commands in a plain text file, save it with the .ps1 extension, and run that file in PowerShell.

A useful first script reads information and prints a result, so you can learn how the pieces fit together without changing Windows settings.

The example here reports how many services are running. Then you'll give it an input, add a clear description, and learn how to change it without turning a small mistake into a much bigger job.

Build a small script before adding automation

On Windows 10 or 11, open Start, search for Windows PowerShell ISE, and open it normally. ISE is the Windows PowerShell 5.1 script editor. Use File > New to create a script tab, then type in its script pane.

If ISE isn't available, a plain text editor can create the same file.

PowerShell 7 uses a separate application. For a project that needs that version, use a plain text editor or Visual Studio Code with its PowerShell extension, then run the file in PowerShell 7. ISE doesn't host the PowerShell 7 engine.

I'd start with a report like this rather than a cleanup script. A report gives you visible results, and an incorrect filter won't delete your files.

# Count the services that are currently running.
$running = Get-Service | Where-Object { $_.Status -eq 'Running' }
$count = @($running).Count
Write-Output "Running services: $count"
Drawing of the PowerShell ISE Save As dialog with the script file type and Save marked

The first line is a comment. PowerShell ignores text after # on that line, which lets you explain why a command is there. Comments are for the next person reading the file, including you in a few weeks.

Get-Service reads the service list. The pipe character, |, passes those service objects to Where-Object, which keeps only the ones with a running status. An object is a result with named details, such as a service's name and status.

Inside the filter, $_ means the service being checked, and -eq means equals. The dollar sign starts a variable name. Think of $running as a named place to keep the filtered results for the next command.

@(...) treats the results as a collection, even for a single result. .Count gets the number of items. The final command prints a sentence with that number; double quotes let PowerShell insert $count into the text.

Save the file as a script, with the right extension

In ISE, choose File > Save As. Enter ServiceCount.ps1, select PowerShell Scripts (*.ps1) as the file type, and click Save. Save it somewhere you'll recognize, such as a Scripts folder inside Documents.

With a general text editor, check that the saved name really ends in .ps1. A file called ServiceCount.ps1.txt is still a text document. In File Explorer, turn on filename extensions to inspect its complete name.

Use plain text rather than a word processor's document format, which also stores things like fonts and page layouts.

Microsoft's script-writing instructions describe a script as commands saved in a plain text file.

Save after each change. Running an old saved copy while looking at new text in an editor is an easy way to misunderstand the result, especially when the console and editor are separate windows.

Run it once and read the result

In ISE, press F5 or choose Run Script. The console pane should show Running services: followed by a number. That number belongs to your PC at that moment; it can change as Windows starts and stops services.

For a separate PowerShell window, first move into the folder containing the file with Set-Location. Then run:

.\ServiceCount.ps1

The .. parent-folder shortcut isn't needed here. The single dot and backslash mean the file is in the current folder. PowerShell doesn't automatically treat a bare filename in that folder as a command.

If a disabled-scripts error appears, use the execution-policy checks before changing anything. Work PCs can have an administrator-set policy that also applies to files you write.

For path errors, other script types, or a script written for a different engine, the Windows script-running steps cover those cases. Keep this first file small until it runs reliably.

Give the script an input instead of editing it each time

A parameter lets the person running the script choose a value. Replace the file's contents with this version, putting param before the commands:

param(
    [string]$Status = 'Running'
)

$services = Get-Service | Where-Object { $_.Status -eq $Status }
$count = @($services).Count
Write-Output "Services with status ${Status}: $count"

[string] means the input is text. The default is Running, so the script still works without an extra argument. The braces around ${Status} keep the following colon separate from the variable name.

Save, then run it from PowerShell with a different value:

.\ServiceCount.ps1 -Status Stopped

The output should now describe stopped services. The script hasn't stopped any services; it only reads the list and counts matches. That's a useful distinction when you're reviewing an unfamiliar command.

This comparison needs a supported status. For a script others will use, limit the accepted values with [ValidateSet('Running','Stopped')] on a line before [string]$Status in the parameter block.

Check mistakes while the file is still easy to follow

Read the first error before the later ones. A missing quote, parenthesis, or closing brace can make several following lines look wrong. Fix the first error, save, and run the same small example again.

Keep the plain text quotes from the code examples. Curved quotes copied from styled text make scripts harder to review, and changing punctuation while copying can alter how PowerShell reads a command.

Try each new command separately before adding it to a loop. For unfamiliar commands, run Get-Help followed by the command name. Check its options so you don't accidentally add a command that changes something to your report.

When a script starts moving or removing files, make a disposable test folder first. Some commands support -WhatIf, which shows what they would change. Check that your command supports it before using it.

If an error remains, give the script's author or your IT team the first error, the relevant few lines, and $PSVersionTable.PSVersion. They can check the code and PowerShell version without needing your whole Documents folder.

The Short Version

Write a few read-only commands, save the file as .ps1, and run it in the matching PowerShell version. Add parameters when the same task needs different inputs, then test each change before making the script do anything destructive.

Checked December 3, 2026.

If you have any business, I can build it online

Hi, I’m Hamza. I’ve been building websites and online shops for business owners since 2019, and I also built this site you’re reading.

Tell me what business you have or what you’re working on. I’ll look into how much it can grow online, and then we talk. You don’t have to buy anything or hire me first.

Chat with me on my site and I’ll show you proof of businesses I’ve helped and how they’ve grown.

Written by Isaac Smith

Isaac Smith is the founder and editor of PC Glance, a website that covers computers, laptops, and technology. He is a tech enthusiast and a computer geek who loves to share his insights and help his readers make smart choices when buying tech gadgets or laptops. He is always curious and updated about the latest tech trends.

More from Isaac Smith

Leave a Comment