How to Run a PowerShell Script (.ps1) on Windows

How to run a PowerShell script (.ps1) on Windows

A coworker in Denver sent me a message one Monday: she’d saved her first cleanup script as a .ps1 file, double-clicked it, and Notepad opened. To run a PowerShell script, open PowerShell in the script’s folder and type its name with .\ in front: In this tutorial, I’ll show you how to run a PowerShell … 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

How to Run PowerShell as a Different User (3 Ways)

Run PowerShell as a different user

To run PowerShell as a different user, start a new PowerShell process with the other account’s credentials. The quickest way is Start-Process with Get-Credential, which asks for the password in a secure prompt: Below I check who you’re running as now, show every way to switch accounts, and explain how this differs from Run as … Read more

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

How to Get Folder Size in PowerShell (Including Subfolders)

Get folder size in PowerShell

To get a folder’s size in PowerShell, list every file under it with Get-ChildItem -Recurse and add up their Length values with Measure-Object. The result is in bytes, so divide by 1MB or 1GB: Output: A folder doesn’t store its own size, so PowerShell has to add up the files, just like the Properties window … Read more

How to Run a PowerShell Script in PowerShell ISE

How to run a PowerShell script in PowerShell ISE

To run a PowerShell script in PowerShell ISE, open the .ps1 file with File > Open (Ctrl+O) and press F5, or click the green Run Script button. To run only part of the script, select those lines and press F8. The output shows in the console pane under the script. The ISE runs Windows PowerShell … Read more

How to Run a PowerShell Script in Visual Studio Code

Run a PowerShell script in Visual Studio Code

To run a PowerShell script in Visual Studio Code, install Microsoft’s PowerShell extension, open your .ps1 file and press F5. To run only a few lines, select them and press F8. You can also type the script path in the terminal: Below I set up the extension, run a script with and without parameters, debug … Read more

Convert Bytes to KB, MB, GB and TB in PowerShell

PowerShell Get-ChildItem showing file sizes converted from bytes to KB and MB

To convert bytes to KB, MB or GB in PowerShell, divide the number by the built-in literal 1KB, 1MB or 1GB. Here’s 5,368,709,120 bytes in gigabytes: Output: In this tutorial, I’ll show you how to convert bytes to KB, MB, GB and TB, round the result, pick the best unit and convert back, tested in … Read more

How to Unzip Files with PowerShell (Expand-Archive)

How to unzip files with PowerShell Expand-Archive

Every Friday, the Portland, Seattle and Boise stores of Cascade Outdoor Supply email Tom Baker a zip of their weekly reports. To unzip a file in PowerShell, run Expand-Archive with the zip’s path and a destination folder: In this tutorial, I’ll turn that one line into Tom’s weekly script and cover each PowerShell unzip file … Read more