Try Catch in PowerShell (With Finally and Examples)

Try catch in PowerShell

In PowerShell, try/catch runs the code in try and jumps to catch when a terminating error happens. For cmdlets, add -ErrorAction Stop so their errors reach the catch block: Output: Inside catch, $_ is the error record, and $_.Exception.Message is the readable message. Below I explain why catch sometimes doesn’t run, catch specific error types, … Read more

How to Use curl in PowerShell: curl.exe vs. the Alias

Use curl in PowerShell

To use curl in PowerShell, type curl.exe instead of curl. Windows 10 and 11 include the real curl, and the .exe makes sure you get it rather than an alias: Output: In Windows PowerShell 5.1, plain curl is an alias for Invoke-WebRequest, so your curl options fail. Below I show the difference, send JSON without … Read more

How to Find the Location of an Executable in PowerShell

Find the location of an executable in PowerShell

To find the location of an executable in PowerShell, run Get-Command and read its Source property. It searches the folders in your PATH, just like running the program does: Output: Below I look up several programs at once, list every copy with -All and where.exe, and find where a running program lives. I used only … Read more

How to Get the Last Reboot Time Using PowerShell

Get the last reboot time with PowerShell

To get the last reboot time with PowerShell, read LastBootUpTime from Win32_OperatingSystem. Subtract it from the current time to get the uptime: Output: Get-CimInstance returns a normal DateTime, so no conversion is needed. Below I show the shorter PowerShell 7 cmdlet, the reboot history from the event log, remote PCs and one Fast Startup catch. … Read more

How to Set a Proxy in PowerShell (7 and 5.1)

Set a proxy in PowerShell

To set a proxy in PowerShell for one request, pass it to -Proxy on Invoke-WebRequest or Invoke-RestMethod. Add -ProxyUseDefaultCredentials if the proxy needs your Windows sign-in: For a whole session, the method differs between PowerShell 7 and Windows PowerShell 5.1. I cover both below, plus bypass lists and the Windows-wide proxy. The proxy server in … Read more

PowerShell Get-ChildItem Filter Examples

PowerShell Get-ChildItem filter examples

To filter files with Get-ChildItem, pass a wildcard pattern to -Filter. Add -Recurse to search subfolders too: Output: -Filter is the fastest option because the file system does the matching. Below I compare it with -Include and -Exclude, show a gotcha that returns nothing, and filter by date and size. All of it runs in … Read more

PowerShell Switch Parameter (With Examples)

PowerShell switch parameter with examples

A PowerShell switch parameter is a parameter of type [switch] that’s on when you include it and off when you leave it out. It takes no value, like -Force or -Recurse: Output: Below I cover how to pass an explicit value, tell whether the caller used the switch, forward it to another function and name … Read more

How to Disable Windows Firewall Using PowerShell

Disable Windows Firewall using PowerShell

To disable Windows Firewall using PowerShell, open PowerShell as administrator and set all three firewall profiles to False: That leaves the PC unprotected, so turn it back on as soon as you’re done. Below I check the status, preview the change with -WhatIf, re-enable it, and show the safer option of changing one rule. My … Read more

How to Set the Default Printer Using PowerShell in Windows

Set the default printer using PowerShell

To set the default printer using PowerShell, get the printer from Win32_Printer and call its SetDefaultPrinter method with Invoke-CimMethod: It works for the current user and doesn’t need admin rights. Below I list printers, check the default, preview the change with -WhatIf and stop Windows from switching it back. My own default stayed untouched. List … Read more