How to Download a File from a URL in PowerShell

Download a file from a URL in PowerShell

To download a file from a URL in PowerShell, run Invoke-WebRequest with -Uri for the address and -OutFile for where to save it: It works in PowerShell 7 and Windows PowerShell 5.1. Below I download a real file, check the response, use BITS for big files, verify the hash and handle errors. For every example … Read more

Lists in PowerShell: How to Use Generic Lists

Lists in PowerShell with System.Collections.Generic.List

To create a list in PowerShell, use the .NET generic List class with the type of item it holds, then call Add() to put items in it: Unlike an array, a list grows and shrinks without being copied. Below I cover adding, finding, removing and sorting items, lists of objects, and when to choose a … Read more

How to Set the Time Zone in Windows with PowerShell

Set the time zone in Windows with PowerShell

To set the time zone in Windows with PowerShell, run Set-TimeZone with the zone’s Id. This switches the PC to Pacific Time: The trick is knowing the exact Id. Below I show how to check the current zone, find the right Id, preview the change, convert times between zones and set the zone on remote … Read more

How to Get Windows Event Logs Using PowerShell

Get Windows event logs using PowerShell

To get Windows event logs using PowerShell, run Get-WinEvent with the log name. This returns the 20 newest events from the System log: For anything bigger, filter with -FilterHashtable so Windows does the filtering before PowerShell sees the events. Below I cover listing logs, filtering by ID, level and date, the Security log, exporting and … Read more

Set a Local User Password in Windows 11 with PowerShell

Set a password for a local user in Windows 11 with PowerShell

To set a password for a local user in Windows 11 with PowerShell, open PowerShell as administrator, read the new password as a secure string and pass it to Set-LocalUser: The password never appears on screen or in your history. Below I show how to check the account first, what the prompt looks like, the … Read more

How to Restart a Computer Using PowerShell

Restart a computer using PowerShell

To restart a computer using PowerShell, run Restart-Computer. It restarts the local PC right away, so save your work first: Add -ComputerName to restart remote PCs. Below I preview restarts safely, restart remote servers and wait for them to come back, check for a pending restart and schedule one for later. Preview a restart with … Read more

How to Save Output to a File in PowerShell

Save output to a file in PowerShell

To save output to a file in PowerShell, pipe it to Out-File, or use the > redirection operator, which does the same thing: Both save the output exactly as it looks on screen. Below I compare every option with the same three sample orders, so you can see which file each one produces. Which method … Read more

How to List Scheduled Tasks with PowerShell

List scheduled tasks with PowerShell

To list scheduled tasks with PowerShell, run Get-ScheduledTask. It returns every task you have permission to see, with its folder, name and state: On a typical PC that’s a couple of hundred tasks. Below I narrow the list by folder, name and state, read run results, see what each task runs, query remote PCs and … Read more

How to List File Names Only in PowerShell

List file names only in PowerShell

To list file names only in PowerShell, run Get-ChildItem with -File and -Name. You get plain names, without folders, sizes or dates: Below I use a small Reports folder with two subfolders to show every variation: recursive lists, names without extensions, filters and saving the list to a file. List names with Get-ChildItem -Name -Name … Read more

How to Pass Variables to a PowerShell Script

Pass variables to a PowerShell script

To pass variables to a PowerShell script, declare them in a param() block at the top of the script, then pass values by name when you run it: Every example below uses one sample script, so you can copy it and follow along. I tested each call in PowerShell 7 and Windows PowerShell 5.1. Declare … Read more