Introduction to PowerShell
What PowerShell is, why it passes objects instead of text, how Windows PowerShell 5.1 differs from PowerShell 7, and the first commands worth knowing, all with real output.
By The Dark DebuggerUpdated 19 min read
This is the first article in the PowerShell path. It explains what PowerShell is, how it differs from text-based shells like Bash, which of the two PowerShell editions you should use, and where people use it at work. You don't need any PowerShell experience. Basic comfort with a terminal helps, and if you know a little Bash the side-by-side examples will land faster. The next article, Installing PowerShell 7, gets it running on your machine.
Why PowerShell exists
Here is a small, realistic job. A web server's logs folder is filling up, and you want every
file bigger than 500 KB with its size. In Bash the usual answer is ls -l piped into awk,
picking columns by position. This is the folder:
ls -l logstotal 6104
-rw-r--r-- 1 sid sid 2420077 Nov 9 21:05 access.log
-rw-r--r-- 1 sid sid 2129964 Nov 8 23:59 access.log.1
-rw-r--r-- 1 sid sid 211648 Nov 7 23:59 access.log.2.gz
-rw-r--r-- 1 sid sid 36023 Nov 9 19:47 error.log
-rw-r--r-- 1 sid sid 926458 Nov 9 21:04 payment service.log
-rw-r--r-- 1 sid sid 514014 Nov 9 20:58 worker.logAnd here is the one-liner most of us would write:
ls -l logs | awk '$5 > 512000 {print $9, $5}'access.log 2420077
access.log.1 2129964
payment 926458
worker.log 514014Look at the third line. The file is called payment service.log, but awk splits on spaces,
so column 9 is just payment. Nothing failed and nothing warned; the answer is simply wrong.
Text pipelines work by guessing where one field ends and the next begins, and every space in a
file name, every locale with a different date format, every tool version that adds a column can
break that guess.
PowerShell takes a different route. Commands don't emit text for the next command to re-parse. They emit objects with named, typed properties. The same job:
Get-ChildItem logs | Where-Object Length -gt 500KB | Select-Object Name, LengthName Length
---- ------
access.log 2420077
access.log.1 2129964
payment service.log 926458
worker.log 514014Length is a property of each file object, not "the fifth column", so the space in the name
can't confuse it. 500KB is a real number (512,000) that PowerShell understands natively. That
difference is the whole reason to learn PowerShell, and the rest of this article builds on it.
What PowerShell actually is
PowerShell is three things at once:
- An interactive shell. You type commands at a prompt, just like Bash or
cmd.exe. - A scripting language. Variables, loops, functions, error handling and modules, all in
.ps1files you can version and review. - An object pipeline on top of .NET. Every value is a .NET object. A date is a
System.DateTime, a file is aSystem.IO.FileInfo, a number is anInt32orDouble.
You can see the third point with the simplest command there is:
Get-Date
Monday, 09 November 2026 21:55:36
That printed text is only a display of the object. The object itself has properties and methods you can use directly:
(Get-Date).DayOfWeek
(Get-Date).AddDays(30).ToString('yyyy-MM-dd')Monday
2026-12-09DayOfWeek is a property, AddDays() is a method, and ToString('yyyy-MM-dd') formats the
result. No date +%Y-%m-%d format strings to memorise per tool, no parsing.
How dates display depends on your culture settings. My test machine for the Linux examples
uses the invariant culture, hence Monday, 09 November 2026. The same Get-Date in Windows
PowerShell on a US-English Windows 11 machine printed this:
Monday, November 9, 2026 9:56:25 PMSame object, different rendering. That is one more reason scripts should work with properties, not with the printed text.
Objects instead of text: a closer look
Let's keep using the logs folder. Plain Get-ChildItem (PowerShell's ls) shows a table:
Get-ChildItem logs
Directory: /home/sid/demo/logs
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
-rw-r--r-- sid sid 11/09/2026 21:05 2420077 access.log
-rw-r--r-- sid sid 11/08/2026 23:59 2129964 access.log.1
-rw-r--r-- sid sid 11/07/2026 23:59 211648 access.log.2.gz
-rw-r--r-- sid sid 11/09/2026 19:47 36023 error.log
-rw-r--r-- sid sid 11/09/2026 21:04 926458 payment service.log
-rw-r--r-- sid sid 11/09/2026 20:58 514014 worker.log
Sorting by size is a matter of naming the property, not knowing which column it lives in:
Get-ChildItem logs | Sort-Object Length -Descending | Select-Object -First 3 Name, Length, LastWriteTime
Name Length LastWriteTime
---- ------ -------------
access.log 2420077 11/09/2026 21:05:00
access.log.1 2129964 11/08/2026 23:59:00
payment service.log 926458 11/09/2026 21:04:00
Because LastWriteTime is a real DateTime, you can do date maths on it without converting
anything. Store one file in a variable and ask it questions:
$log = Get-Item logs/access.log.2.gz
$log.Length
$log.LastWriteTime
$log.LastWriteTime.DayOfWeek211648
Saturday, 07 November 2026 23:59:00
Saturday
How old is that file, in hours? Subtract one date from another and you get a TimeSpan:
((Get-Date) - $log.LastWriteTime).TotalHours45.943674787In Bash this needs stat, date +%s and arithmetic, and the stat flags differ between Linux
and macOS.
Get-Member: what can this object do?
How did I know there was a LastWriteTime property? Get-Member lists everything an object
offers. It is the single most useful discovery command in PowerShell:
$log | Get-Member -MemberType Property
TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property datetime CreationTime {get;set;}
CreationTimeUtc Property datetime CreationTimeUtc {get;set;}
Directory Property System.IO.DirectoryInfo Directory {get;}
DirectoryName Property string DirectoryName {get;}
Exists Property bool Exists {get;}
Extension Property string Extension {get;}
FullName Property string FullName {get;}
IsReadOnly Property bool IsReadOnly {get;set;}
LastAccessTime Property datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;}
LastWriteTime Property datetime LastWriteTime {get;set;}
LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;}
Length Property long Length {get;}
LinkTarget Property string LinkTarget {get;}
Name Property string Name {get;}
UnixFileMode Property System.IO.UnixFileMode UnixFileMode {get;set;}
The TypeName line tells you the object's .NET type, and the Definition column tells you each
property's type. Length is a long, LastWriteTime is a datetime. We'll lean on
Get-Member constantly in Cmdlets, Parameters, and the Help System.
Objects convert cleanly to data formats
Since the data is already structured, turning it into JSON or CSV is one command, with no quoting bugs:
Get-ChildItem logs | Select-Object -First 2 Name, Length, LastWriteTime | ConvertTo-Json[
{
"Name": "access.log",
"Length": 2420077,
"LastWriteTime": "2026-11-09T21:05:00-05:00"
},
{
"Name": "access.log.1",
"Length": 2129964,
"LastWriteTime": "2026-11-08T23:59:00-05:00"
}
]Note that Length came out as a JSON number, not a string, and the date is ISO 8601 with a
time zone offset. That is what makes PowerShell good glue between systems that speak JSON.
Windows PowerShell 5.1 vs PowerShell 7
There are two PowerShells, and mixing them up causes a lot of confusing forum threads.
| Windows PowerShell 5.1 | PowerShell 7 | |
|---|---|---|
| Executable | powershell.exe | pwsh (pwsh.exe on Windows) |
| Runs on | Windows only | Windows, Linux, macOS |
| Built on | .NET Framework 4.x | modern .NET (7.6 runs on .NET 10) |
$PSVersionTable.PSEdition | Desktop | Core |
| Installed by default | Yes, on every Windows 10/11 and Server | No, you install it |
| Development | Security fixes only, no new features | Active, new release every year or so |
They install side by side and don't replace each other. Here is $PSVersionTable from the
Windows PowerShell that ships with Windows 11 on my test laptop:
$PSVersionTableName Value
---- -----
PSVersion 5.1.26100.9711
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.26100.9711
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1And from PowerShell 7 on Ubuntu:
$PSVersionTable
Name Value
---- -----
PSVersion 7.6.9
PSEdition Core
GitCommitId 7.6.9
OS Ubuntu 24.04.1 LTS
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.4
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Which one should you learn? PowerShell 7, and this whole path uses it. Most of what you learn transfers directly to 5.1, and where the two differ I'll show both. A few differences you will hit early:
- PowerShell 7 adds operators 5.1 doesn't have, such as
&&,||,??and the ternary? :. In 5.1,Get-Date && 'ok'is a parse error:The token '&&' is not a valid statement separator in this version. - PowerShell 7 writes files as UTF-8 by default; 5.1 writes UTF-16 with
Out-Fileand>. $IsWindows,$IsLinuxand$IsMacOSexist only in PowerShell 7. In 5.1 they are simply unset, so$IsWindows -eq $nullreturnsTruethere.
You still need 5.1 sometimes. Some older Windows modules only load in 5.1, and on servers you don't control it may be the only PowerShell available. Write for 7, test on 5.1 when a script has to run there.
Verb-Noun: commands you can guess
PowerShell commands are called cmdlets (pronounced "command-lets"), and they are named
Verb-Noun: Get-Process, Stop-Process, Get-ChildItem, Remove-Item. The verbs come from
a fixed, approved list, so once you know the nouns you can guess the commands. Everything that
works with processes:
Get-Command -Noun Process
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Debug-Process 7.0.0.0 Microsoft.PowerShell…
Cmdlet Get-Process 7.0.0.0 Microsoft.PowerShell…
Cmdlet Start-Process 7.0.0.0 Microsoft.PowerShell…
Cmdlet Stop-Process 7.0.0.0 Microsoft.PowerShell…
Cmdlet Switch-Process 7.6.0.500 Microsoft.PowerShell…
Cmdlet Wait-Process 7.0.0.0 Microsoft.PowerShell…
The approved verbs are themselves discoverable:
(Get-Verb).Count
Get-Verb -Verb Get, Set, New, Remove100
Verb AliasPrefix Group Description
---- ----------- ----- -----------
Get g Common Specifies an action that retrieves a resource
New n Common Creates a resource
Remove r Common Deletes a resource from a container
Set s Common Replaces data on an existing resource or creates a resource that contain…
Compare that with Unix, where you have to already know that ps lists processes and kill
stops them. Long names are verbose to type, but tab completion handles the typing, and the names
make scripts readable six months later.
Aliases, and why they differ between systems
For interactive comfort, PowerShell ships short aliases like gci and dir for
Get-ChildItem. On Windows it also adds Unix-looking aliases. This is Windows PowerShell 5.1:
Get-Command ls, cat, curl, sortCommandType Name Version Source
----------- ---- ------- ------
Alias ls -> Get-ChildItem
Alias cat -> Get-Content
Alias curl -> Invoke-WebRequest
Alias sort -> Sort-ObjectOn Linux, PowerShell 7 deliberately does not define those, so ls is the real /usr/bin/ls:
Get-Command ls, gci, Get-ChildItem
CommandType Name Version Source
----------- ---- ------- ------
Application ls 0.0.0.0 /usr/bin/ls
Alias gci -> Get-ChildItem
Cmdlet Get-ChildItem 7.0.0.0 Microsoft.PowerShell…
That's why my rule is: aliases at the prompt, full cmdlet names in scripts. A script that says
ls means something different on each OS; a script that says Get-ChildItem doesn't. We'll go
deeper in Providers, Drives, Aliases, and Native Commands.
Where you'll use this
PowerShell shows up in four kinds of work.
Windows administration. Services, event logs, scheduled tasks, local users, the registry, IIS, Active Directory and Group Policy all have PowerShell cmdlets, and for many tasks the GUI is just a front end over them. A typical check, "which services are set to start automatically but aren't running?", is one pipeline. I ran this read-only check in Windows PowerShell 5.1 on my Windows 11 machine:
Get-Service | Where-Object { $_.StartType -eq 'Automatic' -and $_.Status -ne 'Running' } | Select-Object Name, Status, StartTypeName Status StartType
---- ------ ---------
edgeupdate Stopped Automatic
GoogleUpdaterInternalService157.0.8089.0 Stopped Automatic
GoogleUpdaterService157.0.8089.0 Stopped Automatic
Intel(R) TPM Provisioning Service Stopped Automatic
MapsBroker Stopped Automatic
sppsvc Stopped Automatic
WbioSrvc Stopped AutomaticOn a server you'd run the same pipeline across many machines with remoting, covered in Remoting, Background Jobs, and Parallelism.
Microsoft cloud administration. Azure (the Az modules), Microsoft 365 and Entra ID
(Microsoft.Graph) and Exchange Online (ExchangeOnlineManagement) are managed from PowerShell
modules. Many settings in those services can only be changed, or changed in bulk, from
PowerShell. I didn't run those modules for this article because they need a tenant to talk to,
but they follow exactly the same Verb-Noun and object rules you're learning here.
CI/CD pipelines. GitHub-hosted Actions runners and Microsoft-hosted Azure Pipelines agents
come with PowerShell 7 preinstalled, and a build step that sets its shell to pwsh runs the same script on Windows, Linux and macOS
agents. That cross-platform consistency is often the reason a team picks PowerShell over Bash
for build scripts.
Cross-platform automation. Because objects convert cleanly to JSON, PowerShell is good at "collect some facts, emit structured data" jobs. Here's a complete script that a monitoring agent could run every few minutes against a log folder:
# log-report.ps1 - summarise a log directory as JSON for a monitoring job
param(
[string]$Path = './logs'
)
$files = Get-ChildItem -Path $Path -File
$newest = $files | Sort-Object LastWriteTime -Descending | Select-Object -First 1
$errors = Select-String -Path (Join-Path $Path '*.log') -Pattern ' ERROR '
[pscustomobject]@{
CheckedAt = (Get-Date).ToString('yyyy-MM-ddTHH:mm:ss')
Files = $files.Count
TotalMB = [math]::Round(($files | Measure-Object -Property Length -Sum).Sum / 1MB, 2)
LargestFile = ($files | Sort-Object Length -Descending | Select-Object -First 1).Name
NewestFile = $newest.Name
MinutesQuiet = [int]((Get-Date) - $newest.LastWriteTime).TotalMinutes
ErrorLines = $errors.Count
} | ConvertTo-Json./log-report.ps1 -Path ./logs{
"CheckedAt": "2026-11-09T21:55:37",
"Files": 6,
"TotalMB": 5.95,
"LargestFile": "access.log",
"NewestFile": "access.log",
"MinutesQuiet": 51,
"ErrorLines": 804
}Don't worry about every line yet. param() comes in
Functions and Advanced Functions,
[pscustomobject] in Arrays, Hashtables, and Custom Objects,
and Select-String in Regular Expressions. The point is the
shape: gather objects, compute with their properties, emit structured output. A MinutesQuiet
value that keeps growing is a good sign the application has stopped logging.
How this PowerShell path is organised
The path has five sections, meant to be read in order:
- Getting Started: this article, installing PowerShell 7, and cmdlets, parameters and the help system.
- Core Concepts: the object pipeline, variables and data types, operators, string operators, collections and custom objects, and providers, aliases and native commands.
- Scripting: conditionals, loops, functions, scripts, scope and modules, and error handling.
- Working with Data: files and folders, CSV, JSON and XML, and regular expressions.
- Automation in Practice: REST APIs, remoting, jobs and parallelism, and real-world scripts.
Every example is run in PowerShell 7 on Linux unless it says otherwise. Windows-only examples were run in Windows PowerShell 5.1 using read-only commands.
Common mistakes
Typing Bash flags at a PowerShell alias
On Windows, ls is an alias for Get-ChildItem, so muscle memory from Linux fails. This is
Windows PowerShell 5.1:
ls -laGet-ChildItem : A parameter cannot be found that matches parameter name 'la'.
At line:1 char:4
+ ls -la
+ ~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommandThe alias passes -la to Get-ChildItem, which has no such parameter. The PowerShell way to
"show everything, including hidden files" is Get-ChildItem -Force. And remember the reverse
trap: on Linux, ls -la in pwsh works, because there ls is the real Unix ls.
Running a script by name from the current folder
PowerShell doesn't run scripts from the current directory unless you say so. The first time I
ran a fresh script in an interactive pwsh session, I got this:
rotate-logs.ps1rotate-logs.ps1: The term 'rotate-logs.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
[General Feedback]
The command "rotate-logs.ps1" was not found, but does exist in the current location.
PowerShell does not load commands from the current location by default (see 'Get-Help about_Command_Precedence').
If you trust this command, run the following command instead:
➤ ./rotate-logs.ps1This is a safety feature: a malicious file called ls.ps1 dropped into a folder can't hijack
your command. The fix is in the message itself: prefix the path, ./rotate-logs.ps1 (or
.\rotate-logs.ps1 on Windows; both slashes work there). On Windows you may also meet the
execution policy, which the installation article explains.
Reaching for grep and losing your objects
On Linux you can pipe PowerShell output into grep, and it even looks right:
Get-ChildItem logs | grep access-rw-r--r-- sid sid 11/09/2026 21:05 2420077 access.log
-rw-r--r-- sid sid 11/08/2026 23:59 2129964 access.log.1
-rw-r--r-- sid sid 11/07/2026 23:59 211648 access.log.2.gzBut once objects pass through a native command they become plain strings. Try to sort the result by size:
Get-ChildItem logs | grep access | Sort-Object Length -Descending-rw-r--r-- sid sid 11/07/2026 23:59 211648 access.log.2.gz
-rw-r--r-- sid sid 11/08/2026 23:59 2129964 access.log.1
-rw-r--r-- sid sid 11/09/2026 21:05 2420077 access.logThe smallest file came first. No error, just a wrong answer: each item is now a
System.String, and a string's Length is its number of characters, so the longest line
won. You can confirm the type:
(Get-ChildItem logs | grep access)[0].GetType().FullNameSystem.StringFilter with PowerShell while you still have objects:
Get-ChildItem logs -Filter 'access*' | Sort-Object Length -Descending. Use native tools at the
end of a pipeline, or on text you actually want as text.
Summary
- PowerShell is a shell, a scripting language, and an object pipeline built on .NET. Commands pass objects with typed properties, not lines of text.
- Text pipelines break silently on spaces, locales and changed column layouts. Selecting by
property name (
Where-Object Length -gt 500KB) avoids that whole class of bug. Get-Membershows an object's type, properties and methods. Use it whenever you wonder "what can I do with this?"- Windows PowerShell 5.1 (
powershell.exe,Desktopedition, .NET Framework) ships with Windows and is frozen. PowerShell 7 (pwsh,Coreedition) is cross-platform and actively developed. They run side by side; learn 7. - Cmdlets are named
Verb-Nounfrom a list of 100 approved verbs, soGet-Command -Noun Xfinds everything that works on X. - Unix-style aliases (
ls,cat,curl) exist only on Windows. Use full cmdlet names in scripts so they mean the same thing on every OS. - Scripts in the current folder must be run with a path prefix:
./script.ps1.
Exercises
Print a single line that says which PowerShell version and edition you're running and on which
OS, for example PowerShell 7.6.9 (Core) on Ubuntu 24.04.1 LTS. Use $PSVersionTable.
Hint
Inside double quotes, $( … ) runs an expression and inserts the result. $PSVersionTable
has PSVersion, PSEdition and OS entries.
Show Solution
"PowerShell $($PSVersionTable.PSVersion) ($($PSVersionTable.PSEdition)) on $($PSVersionTable.OS)"PowerShell 7.6.9 (Core) on Ubuntu 24.04.1 LTSIn Windows PowerShell 5.1 the OS entry doesn't exist, so that part comes out empty. That's a
small, real example of the editions differing.
Using Get-Date and date subtraction, print how many whole days are left until
1 January 2027.
Show Solution
((Get-Date '2027-01-01') - (Get-Date)).Days52I ran this on the evening of 9 November 2026. Subtracting two DateTime objects gives a
TimeSpan, and its Days property is the whole-day part. Your number will depend on the day
you run it.
You want to turn objects into some text format but can't remember which formats PowerShell
supports. Without searching the web, list every command whose verb is ConvertTo.
Show Solution
Get-Command -Verb ConvertTo
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet ConvertTo-CliXml 7.0.0.0 Microsoft.PowerShell…
Cmdlet ConvertTo-Csv 7.0.0.0 Microsoft.PowerShell…
Cmdlet ConvertTo-Html 7.0.0.0 Microsoft.PowerShell…
Cmdlet ConvertTo-Json 7.0.0.0 Microsoft.PowerShell…
Cmdlet ConvertTo-SecureString 7.0.0.0 Microsoft.PowerShell…
Cmdlet ConvertTo-Xml 7.0.0.0 Microsoft.PowerShell…
This is the Verb-Noun payoff: you searched by intent (convert to something) rather than by a
name you had to already know. Get-Command -Verb ConvertFrom finds the reverse direction.
In Bash you might run ls -lS logs | head -4 to see the biggest files. Write the PowerShell
equivalent that shows the three largest files in logs with their name and size, and explain
why it can't be fooled by the space in payment service.log.
Show Solution
Get-ChildItem logs | Sort-Object Length -Descending | Select-Object -First 3 Name, Length
Name Length
---- ------
access.log 2420077
access.log.1 2129964
payment service.log 926458
Sort-Object compares the numeric Length property of each file object, and Select-Object
picks properties by name. Nothing is ever split on whitespace, so file names with spaces are
just values.
Print how many hours ago logs/access.log.2.gz was last written, rounded to one decimal place,
as a single command.
Hint
(Get-Item path).LastWriteTime is a DateTime. Subtracting it from Get-Date gives a
TimeSpan with a TotalHours property. [math]::Round(value, 1) rounds.
Show Solution
[math]::Round(((Get-Date) - (Get-Item logs/access.log.2.gz).LastWriteTime).TotalHours, 1)45.9The file was last written on 7 November at 23:59 and I ran this late on 9 November, so about
46 hours is right. Use TotalHours, not Hours: Hours is only the hours component
(0 to 23) of the TimeSpan and would have said 21 here.
Tested with: PowerShell 7.6.9 on Ubuntu 24.04 (WSL2), and Windows PowerShell
5.1.26100.9711 on Windows 11, November 2026. Linux examples ran in pwsh in a 100-column
terminal against a demo logs folder; the Bash comparison used GNU Bash 5.2.21 on the same
machine. The Windows examples ($PSVersionTable, aliases, ls -la, the service check and the
US-English Get-Date display) were read-only commands run in Windows PowerShell 5.1. The
rotate-logs.ps1 error came from an interactive pwsh session, because the "General Feedback"
suggestion only appears interactively. The Azure and Microsoft 365 modules were not run. Paths from the test machine's scratch directories are shown shortened to /home/sid/….
Written by The Dark Debugger
Engineer documenting the things that took me too long to learn, so they take you less time. Every PowerShell tutorial here is tested end to end before publishing.