How to run powershell script from cmd

By FoxLearn 11/18/2024 6:33:00 AM   128
To run a PowerShell script from the command line, you can follow these steps.

How to execute PowerShell Scripts from CMD using Script Path?

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

For example how to run powershell script from command line:

# 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?

You can execute the PowerShell script by calling powershell from the Command Prompt and providing the path to the script.

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\"'}"

If you want to get system information

powershell -Command "Get-ComputerInfo"

If the script is blocked by the execution policy, you can set the execution policy for the current session to allow scripts to run by adding the -ExecutionPolicy Bypass flag as shown above.

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.