# PowerShell — Object-Based Shell and Scripting Language from Microsoft

> Practical guide to PowerShell — cmdlets, the object-based pipeline, processes, services and scripts across platforms with pwsh 7.

Source: https://www.jpkc.com/db/en/cheatsheets/shell-system/powershell/

<!-- PROSE:intro -->
PowerShell is more than a shell: its pipeline passes real .NET objects rather than plain text lines – the key difference from bash. Commands follow the clear `Verb-Noun` scheme like `Get-Process` or `Get-ChildItem`, and `Get-Help` plus `Get-Command` let you discover the rest yourself. Since PowerShell 7 (`pwsh`) it runs cross-platform on Windows, Linux and macOS. This guide covers the cmdlets for navigation, processes, the pipeline and scripting that you reach for every day.
<!-- PROSE:intro:end -->

## Basics & Navigation

`Get-Location` — Show the current directory (alias: pwd).

```bash
Get-Location
```

`Set-Location <path>` — Change directory (alias: cd).

```bash
Set-Location C:\Users
```

`Get-ChildItem` — List files and directories (alias: ls, dir).

```bash
Get-ChildItem -Recurse -Filter *.log
```

`Get-Content <file>` — Display file contents (alias: cat, type).

```bash
Get-Content C:\logs\app.log -Tail 20
```

`Get-Command <name>` — Find commands by name or pattern.

```bash
Get-Command *-Service
```

`Get-Help <cmdlet>` — Show help for a cmdlet.

```bash
Get-Help Get-Process -Examples
```

`Get-Alias <alias>` — Show what a command alias maps to.

```bash
Get-Alias ls
```

## Processes & Services

`Get-Process` — List all running processes.

```bash
Get-Process | Sort-Object CPU -Descending | Select -First 10
```

`Stop-Process -Name <name>` — Kill a process by name.

```bash
Stop-Process -Name notepad
```

`Stop-Process -Id <pid>` — Kill a process by PID.

```bash
Stop-Process -Id 1234 -Force
```

`Get-Service` — List all services.

```bash
Get-Service | Where-Object Status -eq Running
```

`Start-Service <name>` — Start a service.

```bash
Start-Service -Name wuauserv
```

`Stop-Service <name>` — Stop a service.

```bash
Stop-Service -Name Spooler
```

`Restart-Service <name>` — Restart a service.

```bash
Restart-Service -Name nginx
```

## Files & Text

`Copy-Item <src> <dest>` — Copy files or directories (alias: cp, copy).

```bash
Copy-Item -Path .\config -Destination .\backup -Recurse
```

`Move-Item <src> <dest>` — Move or rename files (alias: mv, move).

```bash
Move-Item old.txt new.txt
```

`Remove-Item <path>` — Delete files or directories (alias: rm, del).

```bash
Remove-Item -Path .\temp -Recurse -Force
```

`New-Item -ItemType File <name>` — Create a new file.

```bash
New-Item -ItemType File -Name config.json
```

`New-Item -ItemType Directory <name>` — Create a new directory (alias: mkdir).

```bash
New-Item -ItemType Directory -Name logs
```

`Select-String -Pattern '<regex>' -Path <files>` — Search for text in files (like grep).

```bash
Select-String -Pattern 'error' -Path *.log -CaseSensitive
```

## Pipeline & Filtering

`| Where-Object { <condition> }` — Filter objects in the pipeline.

```bash
Get-Process | Where-Object { $_.CPU -gt 100 }
```

`| Select-Object <properties>` — Select specific properties from objects.

```bash
Get-Process | Select-Object Name, CPU, WorkingSet | Sort-Object CPU -Descending
```

`| Sort-Object <property>` — Sort objects by a property.

```bash
Get-ChildItem | Sort-Object Length -Descending
```

`| Format-Table` — Display output as a table.

```bash
Get-Service | Format-Table Name, Status, StartType
```

`| Format-List` — Display output as a detailed list.

```bash
Get-Process explorer | Format-List *
```

`| Measure-Object` — Count, sum, average, min, max of objects.

```bash
Get-ChildItem *.log | Measure-Object -Property Length -Sum
```

`| Export-Csv <file>` — Export pipeline output to CSV.

```bash
Get-Process | Export-Csv processes.csv -NoTypeInformation
```

`| ConvertTo-Json` — Convert output to JSON format.

```bash
Get-Service | ConvertTo-Json | Out-File services.json
```

## System & Network

`Get-ComputerInfo` — Show detailed system information.

```bash
Get-ComputerInfo | Select OsName, OsVersion, CsTotalPhysicalMemory
```

`Get-NetIPAddress` — Show IP addresses of all network interfaces.

```bash
Get-NetIPAddress -AddressFamily IPv4
```

`Test-NetConnection <host> -Port <port>` — Test network connectivity (like telnet/ping).

```bash
Test-NetConnection google.com -Port 443
```

`Invoke-WebRequest <url>` — Download a web page or file (alias: curl, wget).

```bash
Invoke-WebRequest -Uri https://example.com -OutFile page.html
```

`Invoke-RestMethod <url>` — Call a REST API and parse the JSON response.

```bash
Invoke-RestMethod -Uri https://api.github.com/repos/microsoft/terminal
```

`Get-WinEvent -LogName System -MaxEvents 20` — Show recent event log entries (Windows).

```bash
Get-WinEvent -LogName System -MaxEvents 20
```

## Variables & Scripting

`$variable = <value>` — Set a variable.

```bash
$name = 'World'; Write-Host "Hello $name"
```

`$env:<VAR>` — Access environment variables.

```bash
$env:PATH; $env:COMPUTERNAME
```

`if (<condition>) { } else { }` — Conditional execution.

```bash
if (Test-Path .\config.json) { 'Found' } else { 'Missing' }
```

`foreach ($item in $collection) { }` — Loop over a collection.

```bash
foreach ($f in Get-ChildItem *.txt) { Write-Host $f.Name }
```

`Set-ExecutionPolicy RemoteSigned` — Allow running local scripts (required once).

```bash
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
```

`.\<script>.ps1` — Run a PowerShell script.

```bash
.\deploy.ps1
```

<!-- PROSE:outro -->
## Conclusion

PowerShell combines an interactive shell with a full scripting language – once the object-based pipeline model clicks, you filter and format data with `Where-Object`, `Select-Object` and `Format-Table` far more precisely than with plain text processing. For cross-platform work, reach for PowerShell 7 (`pwsh`); the older Windows PowerShell 5.1 stays Windows-only and carries legacy cmdlets like `Get-EventLog`. Mind the execution policy: `RemoteSigned` is a sensible default that allows local scripts but requires downloaded ones to be signed – don't reach for `Bypass` or `Unrestricted` lightly, and review third-party scripts before you run them.

## Further Reading

- [Microsoft Learn: PowerShell documentation](https://learn.microsoft.com/en-us/powershell/) – official documentation for PowerShell and every cmdlet
- [Microsoft Learn: about_Execution_Policies](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies) – understand and safely configure execution policies
<!-- PROSE:outro:end -->

## Related Commands

- [bash](https://www.jpkc.com/db/en/cheatsheets/shell-system/bash/) – the GNU Bourne-Again Shell, the default shell on Linux
- [zsh](https://www.jpkc.com/db/en/cheatsheets/shell-system/zsh/) – the Z shell with advanced completion and plugins
- [wsl](https://www.jpkc.com/db/en/cheatsheets/shell-system/wsl/) – Windows Subsystem for Linux, bridging PowerShell and Linux shells

