How to Exit a PowerShell Script (exit, return, break)

PowerShell 7 console showing a script that stops with exit 2 and the $LASTEXITCODE value

To exit a PowerShell script, use the exit keyword, optionally with an exit code such as exit 2. The script stops on that line, and the caller reads the number from $LASTEXITCODE. Here’s Check-Backup.ps1: Without a Backups folder, it stops early: Output: In this tutorial, I’ll show you how to exit a PowerShell script with … Read more

Invoke-Sqlcmd: Run SQL Server Queries from PowerShell

Invoke-Sqlcmd running a SQL Server query from PowerShell

Your weekly sales numbers live in SQL Server, and you’re tired of opening SQL Server Management Studio just to copy them into a spreadsheet. PowerShell can ask the database directly and hand the rows back as objects. Quick answer: use Invoke-Sqlcmd from the SqlServer module to run a SQL query in PowerShell. Give it the … Read more

How to Run a Batch File from PowerShell (5 Methods)

Run a batch file from PowerShell with the call operator

To run a batch file from PowerShell, type the call operator and the file’s full path in quotes, then any arguments: Inside its folder, .\backup.bat works too. Plain backup.bat fails, as you’ll see. In this tutorial, I’ll show you five ways to run a batch file from PowerShell, plus arguments, exit codes, output and environment … Read more

PowerShell Aliases: List, Create and Remove

PowerShell alias count and the curl and wget aliases checked in PowerShell 7

Short on keystrokes? A PowerShell alias is a nickname for a command. Set-Alias creates one, and Get-Alias shows what it points to: In this tutorial, I’ll show you how to list, create, protect, remove and keep a PowerShell alias, and where PowerShell 7 and Windows PowerShell 5.1 disagree. What is a PowerShell alias? An alias … Read more

Run a PowerShell Script on a Remote Computer (Invoke-Command)

Invoke-Command PowerShell parameters checked in PowerShell 7

To run a PowerShell script on a remote computer, use Invoke-Command with -ComputerName and either a scriptblock or -FilePath. This one line sends a local .ps1 file to a sales PC and runs it there: In this tutorial, I’ll show you how Invoke-Command in PowerShell works with one or many computers, how to pass local … Read more

Run a PowerShell Script with Task Scheduler (Step by Step)

Run a PowerShell script from Task Scheduler: Edit Action with powershell.exe and -File arguments

To run a PowerShell script from Task Scheduler, don’t point the task at the .ps1 file. Point it at powershell.exe and pass the script with -File, using this in the task’s Add arguments box: In this tutorial, I’ll show you how to run a PowerShell script from Task Scheduler by building a real daily task, … Read more

PowerShell Sleep: Pause a Script with Start-Sleep

PowerShell sleep with Start-Sleep -Seconds measured by a stopwatch

A nightly order import for an Austin coffee roaster kept failing because it read the vendor’s file before the upload finished. The PowerShell sleep command fixed it: Start-Sleep pauses the script, then the next line runs. In this tutorial, I’ll show you how PowerShell sleep works with seconds, milliseconds and a TimeSpan, how to wait … Read more

How to Use 7-Zip from the Command Line in PowerShell

Using the 7-Zip command line (7z.exe) in PowerShell

Our Denver office sends three city sales folders to an auditor monthly, and Compress-Archive can’t add a password. To use the 7-Zip command line in PowerShell, call 7z.exe by its full path with the & operator: In this tutorial, I’ll walk you through the 7-Zip command line from PowerShell: archives, passwords, exit codes and the … 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