How to Run a PowerShell Script from CMD (With Parameters)

Run a PowerShell script from cmd with parameters

To run a PowerShell script from cmd, type powershell with -File and the script path, then any parameters: Swap powershell for pwsh to use PowerShell 7. I ran every command below in a real Command Prompt window, and the two versions don’t always agree on paths, switches and exit codes. Run a PowerShell script from … Read more

How to Run a PowerShell Script as Administrator

How to run a PowerShell script as administrator on Windows

Your script restarts a service or edits a protected folder, and it stops with “Access is denied.” You’re signed in as an admin, yet the script still doesn’t have admin rights. Quick answer: to run a PowerShell script as administrator, open PowerShell with Run as administrator and run the script there, or start an elevated … Read more

How to Call a PowerShell Script from Another Script

Call a PowerShell script from another script with the call operator

If you have a main script that needs to run a helper script, put the call operator and the helper’s full path on one line: & “$PSScriptRoot\Jobs\Get-OpenInvoices.ps1”. That’s how you call a PowerShell script from another script. The line itself is easy. The trouble starts around it: relative paths that break when you launch from … Read more

How to Create a PowerShell Script Step by Step

How to create a PowerShell script step by step

Dana Reyes, who runs the office at Harbor Point Logistics in Seattle, kept asking how full the shared Reports folder was. Here’s how to create a PowerShell script for that: type the commands in a text editor and save them with a .ps1 extension: In this tutorial, I’ll show you how to create a PowerShell … 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 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