How to run powershell script from cmd

By FoxLearn 10/3/2024 2:08:31 AM   37
You can run a PowerShell script from the Command Prompt (CMD) by using the script path or PowerShell command, Here’s a brief summary of the steps.

How to execute PowerShell Scripts from CMD using Script Path?

Open your notepad, and then enter the command as shown below.

For example:

# script.ps1 display current date/time
Write-Host "Current Date and Time: $currentDateTime"

Next, Press Win + R, type cmd, and hit Enter.

Use the following command format:

powershell -ExecutionPolicy Bypass -File "C:\path\script.ps1"

How to execute PowerShell Scripts from CMD using PowerShell Command?

Press Win + R, type cmd, and hit Enter.

Use the following command format if you want to execute a script.

powershell -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File \"C:\path\script.ps1\"'}"

or if you want to get system information

powershell -Command "Get-ComputerInfo"

The -ExecutionPolicy Bypass option allows the script to run without being blocked by execution policy restrictions. You can adjust this based on your needs.

If your script's path contains spaces, ensure you enclose it in double quotes.

If your script requires administrative privileges, make sure to run CMD as an administrator.