M40 - PowerShell Essentials
PowerShell Essentials
Write and run small PowerShell scripts using variables, conditionals, loops, and the object pipeline.
- Write and run small PowerShell scripts using variables, conditionals, loops, and the object pipeline.
PowerShell as a Practical Windows Tool
PowerShell is useful because it helps you describe Windows system tasks in a consistent way.
The most important idea is not “PowerShell is powerful.” It is that PowerShell works with objects. When a command outputs data, you often get named properties such as Name, Path, CPU, or Status instead of a block of unstructured text.
That makes PowerShell especially good for:
- inspecting Windows state
- filtering objects by property
- building small repeatable admin and support tasks
1. Running Your First Script Safely
Windows blocks many .ps1 files by default. That is intentional. It reduces the chance of accidentally running an untrusted script.
For personal learning on your own machine, a common starting point is:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Use this carefully:
- apply it only on a machine you control or a lab machine
- do not change organization policy on managed devices without permission
- prefer
CurrentUserover broader scopes when learning
Now create a tiny script file, for example hello.ps1:
$Workspace = “$HOME\Documents\os-practice” Write-Host “Checking workspace: $Workspace”
Run it from PowerShell with:
.\hello.ps1
The .\ matters. It tells PowerShell to run the script from the current folder instead of searching elsewhere.
2. Variables and Simple Decisions
PowerShell variables begin with $. Use them for the parts of the script that may change.
$Workspace = “$HOME\Documents\os-practice”
if (-not (Test-Path $Workspace)) { New-Item -ItemType Directory -Path $Workspace | Out-Null Write-Host “Created $Workspace” } else { Write-Host “Workspace already exists”
}This is a good beginner pattern:
- store the important path in one variable
- check the current state
- only then make a change
3. Loops for Repeated Work
Loops help when you want to perform the same check or action on many items.
$Target = “$HOME\Documents”
Get-ChildItem -Path $Target -File | ForEach-Object { Write-Host “Found file: $($_.Name)”
}This script is simple, but the idea scales well. Once you can loop through files, services, or processes, you can build reports and checks instead of manually clicking through the same view over and over.
4. The Object Pipeline
PowerShell’s pipeline passes objects, not just text. That means you can filter on properties directly.
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 Name, CPU
And if you want to inspect the shape of those objects:
Get-Process | Get-Member
Get-Member is one of the safest and most useful commands in PowerShell. It helps you learn what properties exist before you write filters or automation around them.
Inspect Before You Automate
In PowerShell, the best habit is to explore the object first, then automate around what you actually found. Use Get-Member, Select-Object, and small reporting commands before you reach for anything that changes or stops a system resource.
5. A Safe Pattern for Small Scripts
As your scripts grow, keep them easy to reason about:
- define the path, name, or input you care about
- inspect the object or folder
- filter to the items you want
- print or save a useful result
That pattern gives you a script you can trust and revise.
What You Just Learned
- PowerShell scripts are usually run with
.\script.ps1. Set-ExecutionPolicy RemoteSigned -Scope CurrentUseris a common learning setup on personal machines.- Variables and conditionals help you write safer, reusable scripts.
- Loops let you repeat the same check or action across many items.
- The PowerShell pipeline passes objects with properties, not just raw text.
Get-Memberis one of the best tools for understanding what a command actually returned.
Next, you will learn the equivalent scripting foundation in Bash on Linux.