Skip to content

PowerShell and Winget Cheat Sheet

Copy-paste commands with a one-line explanation each. Run PowerShell as Administrator where noted. Read each command before running it, especially anything that restarts services or installs software. New to this pillar? Start at SysAdmin Start Here, or build a full install script with the Script Stack Generator.

Command What it does
winget search vscode Search the configured sources for a package
winget show Microsoft.VisualStudioCode Show details for a package ID
winget install --id Microsoft.VisualStudioCode -e Install by exact ID (-e means exact match)
winget list List installed apps that winget can see
winget upgrade List apps with available updates
winget uninstall --id <ID> Remove an app by ID
Terminal window
winget upgrade --all

Upgrades all apps that have updates. Review the list first with winget upgrade.

Terminal window
# On the old machine: write installed apps to a file
winget export -o apps.json
# On the new machine: install everything from the file
winget import -i apps.json --accept-package-agreements --accept-source-agreements

Edit apps.json before importing to remove apps you do not want. Some apps may fail to import if they are not available in your sources; the command reports which ones.

Terminal window
Get-NetTCPConnection -State Listen

Lists listening TCP ports with the owning process ID.

Terminal window
Get-NetTCPConnection -State Listen |
Select-Object LocalAddress, LocalPort, OwningProcess,
@{Name='Process'; Expression={ (Get-Process -Id $_.OwningProcess).ProcessName }} |
Sort-Object LocalPort

Adds a process name column. Some system processes may need an elevated prompt to resolve.

Terminal window
Get-NetTCPConnection -LocalPort 1883 | Select-Object State, OwningProcess
Get-Process -Id <PID>

Replace 1883 with your port, then look up the PID it returns.

Terminal window
Test-NetConnection 192.168.1.20 -Port 502

Look for TcpTestSucceeded : True. A False can mean a closed port, a firewall, or a wrong address. Port numbers for common industrial and IoT protocols are in the ports cheat sheet.

Command What it does
Get-Service List all services and their state
Get-Service -Name Spooler Show one service
Get-Service | Where-Object Status -eq 'Running' Only running services
Restart-Service -Name Spooler Restart a service (admin)
Stop-Service -Name Spooler / Start-Service -Name Spooler Stop or start a service (admin)
Get-Service -Name Spooler | Select-Object * All properties, including dependencies
Terminal window
# System errors in the last 24 hours
Get-WinEvent -FilterHashtable @{
LogName = 'System'
Level = 2
StartTime = (Get-Date).AddHours(-24)
} | Select-Object TimeCreated, Id, ProviderName, Message -First 20

Level values: 1 Critical, 2 Error, 3 Warning, 4 Information.

Terminal window
# Service Control Manager events (service crashes and state changes)
Get-WinEvent -FilterHashtable @{
LogName = 'System'
ProviderName = 'Service Control Manager'
StartTime = (Get-Date).AddDays(-1)
} | Select-Object TimeCreated, Id, Message -First 20
Terminal window
# Failed logons (Security log requires an elevated prompt)
Get-WinEvent -FilterHashtable @{ LogName='Security'; Id=4625 } -MaxEvents 20

Event ID 4625 is a failed logon. If you see many from one source, treat it as a security matter; see Cybersecurity start.

Command What it does
systemctl status NAME State of a service plus the last few log lines
sudo systemctl restart NAME Restart a service
sudo systemctl enable --now NAME Start now and on every boot
systemctl list-units --type=service --state=failed Show failed services
journalctl -u NAME -f Follow a service’s log live
journalctl -u NAME --since "1 hour ago" Recent log entries only
journalctl -p err -b Errors since the current boot

For network commands on Linux (ip, ss, ping), see the Linux networking CLI cheat sheet.

Command What it does
docker ps Running containers (add -a to include stopped)
docker logs -f --tail 100 NAME Follow the last 100 log lines of a container
docker stats Live CPU, memory and network use per container
docker stats --no-stream One snapshot, then exit
docker inspect NAME Full JSON configuration and state
docker inspect -f '{{.State.Status}}' NAME Pull out a single field
docker exec -it NAME sh Open a shell inside a running container
docker restart NAME Restart a container

Practise these on a safe target such as the virtual Modbus Docker lab.

Service down?
|
+-- Windows: Get-Service -> Get-WinEvent (System, last 24 h) -> Restart-Service
+-- Linux: systemctl status -> journalctl -u NAME -> restart
+-- Docker: docker ps -a -> docker logs -> docker inspect
|
+-- Still failing? Is the port listening (Get-NetTCPConnection / ss -tulpn)
and reachable from the client (Test-NetConnection)?

Next: assemble these into a repeatable script with the Script Stack Generator, and keep the Tools hub handy.