Skip to main content

How to Run PowerShell Commands from a Batch Script

While the traditional batch scripting environment (cmd.exe) is powerful for simple file and system operations, it has significant limitations. It cannot handle complex tasks like parsing JSON or XML, making web requests, or performing floating-point math. For these modern challenges, you need a more powerful tool. Fortunately, every modern version of Windows comes with one built-in: PowerShell.

This guide will teach you how to create powerful hybrid scripts by calling PowerShell from within your batch files. This "best of both worlds" approach allows you to use the simple, familiar syntax of a .bat file while delegating the heavy lifting to the far more capable PowerShell engine. You will learn how to execute simple one-liners, how to run entire .ps1 script files, and most importantly how to capture the output from PowerShell back into your batch script's variables.

The Core Command: powershell.exe

The powershell.exe executable is your gateway to the PowerShell engine. From a batch script, you can call it like any other command-line tool. Its switches allow you to pass a command or a script file for it to execute.

Method 1: Running a Simple One-Liner Command

This is the most common use case, where you need PowerShell to perform a single, specific task that is difficult or impossible in batch.

Syntax: powershell -Command "Your-PowerShell-Code-Here"

For example, this script uses PowerShell to get the current date and time in a specific, sortable format (ISO 8601), something that is very complex to do reliably in pure batch.

@ECHO OFF
ECHO --- Getting the date with PowerShell ---
ECHO.
powershell -Command "Get-Date -Format 'yyyy-MM-dd HH:mm:ss'"

PowerShell executes the command, prints the result to the console, and then exits, returning control to the batch script.

Method 2: Capturing PowerShell Output into a Batch Variable

This is the most powerful technique for hybrid scripting. You run a PowerShell command and capture its result into a batch variable for later use. This is done by wrapping the powershell.exe call in a FOR /F loop.

For example, this script gets the day of the week and stores it in a batch variable.

@ECHO OFF
SET "DayOfWeek="

FOR /F "delims=" %%V IN ('powershell -Command "(Get-Date).DayOfWeek"') DO (
SET "DayOfWeek=%%V"
)

ECHO According to PowerShell, today is %DayOfWeek%.

How it works: The FOR /F command executes the command in the single quotes, captures its text output, and the SET command inside the loop assigns that output to the DayOfWeek variable.

Method 3: Running an External PowerShell Script (.ps1) File

For complex logic, you can write a full PowerShell script (.ps1 file) and have your batch file execute it.

Syntax: powershell -File "C:\Path\To\MyScript.ps1"

This is the launcher batch script:

Launcher.bat
@ECHO OFF
ECHO --- Running an external PowerShell script ---
powershell -File "MyPowerShellScript.ps1"
ECHO --- PowerShell script has finished ---

And this one is the launched PowerShell Script

MyPowerShellScript.ps1
Write-Host "Hello from PowerShell!"
$user = $env:USERNAME
Write-Host "This script is being run by: $user"

Key powershell.exe Parameters for Batch Scripts

  • -Command: Executes the command string that follows.
  • -File: Executes a .ps1 script file.
  • -ExecutionPolicy Bypass: (Highly Recommended) This is a crucial switch. It tells PowerShell to run your script regardless of the system's current execution policy, which is a safety feature that can prevent scripts from running. Using Bypass makes your script much more portable and reliable.
  • -NoProfile: Starts PowerShell without loading the user's profile scripts, which makes it start faster and provides a cleaner, more predictable environment.

Recommended Syntax for Scripts: powershell -NoProfile -ExecutionPolicy Bypass -Command "..."

Common Pitfalls and How to Solve Them

  • Execution Policy: This is the number one reason a PowerShell script fails to run from batch. If the system's policy is "Restricted" (the default on some systems), your command will be blocked. Solution: Always use -ExecutionPolicy Bypass in your batch file call.

  • Quotes within Quotes: This can be tricky. Your PowerShell command is inside a double-quoted string for batch. If your PowerShell code also needs quotes, it's best to use single quotes inside the command string.

    • GOOD: powershell -Command "Get-Content 'C:\My Path\file.txt'"
    • BAD: powershell -Command "Get-Content "C:\My Path\file.txt"" (This will break)
  • Administrator Rights: If the PowerShell command you are running requires elevation (e.g., Stop-Service), your batch script must be run as an Administrator.

Practical Example: Getting the Content of a Web Page

This is a task that is completely impossible in pure batch but trivial in PowerShell. This script downloads the content of a web page and stores it in a batch variable.

@ECHO OFF
SETLOCAL
SET "URL=https://api.ipify.org"
SET "MyPublicIP="

ECHO --- Getting Public IP Address from a Web Service ---
ECHO.

FOR /F "delims=" %%I IN (
'powershell -NoProfile -ExecutionPolicy Bypass -Command "(New-Object System.Net.WebClient).DownloadString(''%URL%'')"'
) DO (
SET "MyPublicIP=%%I"
)

IF DEFINED MyPublicIP (
ECHO [SUCCESS] Your public IP address is: %MyPublicIP%
) ELSE (
ECHO [FAILURE] Could not retrieve the IP address.
)
ENDLOCAL

Conclusion

Calling PowerShell from a batch script is the ultimate "power move" for any scripter working in a Windows environment. It allows you to overcome the inherent limitations of cmd.exe by delegating complex tasks to a modern, powerful engine.

  • Use powershell -Command "..." for simple, single-line commands.
  • Use FOR /F to capture the output of a PowerShell command into a batch variable. This is the key to a true hybrid script.
  • Use powershell -File "..." to execute more complex, external .ps1 files.
  • Always use the -ExecutionPolicy Bypass and -NoProfile switches for maximum reliability and performance.