How to Run PowerShell Script From Command Line With Parameters?

Do you want to know how to run PowerShell script from command line with parameters? In this PowerShell tutorial, I will explain how to run a PowerShell script in cmd and how to run a ps1 file in PowerShell as administrator.

Run PowerShell script in cmd

Running a PowerShell script from the Command Prompt (CMD) is a straightforward process, but before you can run scripts, you must ensure that your system’s execution policy allows it.

Step 1: Set Execution Policy

PowerShell scripts can be blocked from running by default as a security measure. You’ll need to set the execution policy to allow scripts to run.

  1. Open PowerShell as an administrator by searching for “PowerShell” in the Start menu, right-clicking on the Windows PowerShell, and selecting “Run as administrator”.
  2. Set the execution policy with the following command:Set-ExecutionPolicy Unrestricted This command will allow all PowerShell scripts to run. Be cautious with this setting as it could pose a security risk. For a more secure setting, you can use RemoteSigned which allows running scripts that are created on your local machine or signed by a trusted publisher.

Step 2: Write Your PowerShell Script

  1. Open your text editor of choice.
  2. Write your PowerShell script. For example:Write-Host "Hello, World!"
  3. Save the file with a .ps1 extension, for example, MyScript.ps1.

Step 3: Run the PowerShell Script from CMD

  1. Open the Command Prompt by typing cmd in the Start menu.
  2. Navigate to the directory where your script is saved using the cd command. For example:cd C:\Path\To\Your\Script
  3. Run the PowerShell script by invoking PowerShell and passing the script path as an argument:powershell -ExecutionPolicy Bypass -File "C:\Path\To\Your\Script\MyScript.ps1" The -ExecutionPolicy Bypass parameter allows you to bypass the execution policy for this session without changing the system-wide policy.

Step 4: Understanding Command Prompt Output

After running the script, any output generated by the script will be displayed in the Command Prompt window. If your script includes a Write-Host command, you’ll see the text it outputs directly in the CMD window.

By following these steps, you can write and run PowerShell scripts from the Command Prompt. Remember to revert your execution policy to a safer setting if you changed it to Unrestricted after you’re done running your scripts.

Read How to Run PowerShell Script in Visual Studio Code

Run PowerShell script from the command line with parameters

Running a PowerShell script from the command line with parameters involves passing arguments to the script when you call it. Here’s how to do it:

Step 1: Write a PowerShell Script with Parameters

  1. Open a text editor and create a new PowerShell script file with a .ps1 extension.
  2. Define parameters at the beginning of your script using the param keyword. For example:param ( [string]$Name, [int]$Age ) Write-Host "Hello, $Name! You are $Age years old."
  3. Save the script, for example, as MyScriptWithParams.ps1.

Step 2: Run the Script from the Command Line with Parameters

  1. Open Command Prompt (CMD).
  2. Navigate to the directory where your script is located using the cd command.
  3. Run the script by calling PowerShell and passing the script path along with the parameters. You can pass parameters by simply appending them after the script path, separated by spaces:powershell -File "C:\Path\To\Your\Script\MyScriptWithParams.ps1" -Name "John" -Age 30 In this example, “John” is passed to the $Name parameter, and 30 is passed to the $Age parameter.

Understanding Parameters in PowerShell

  • Parameters are defined in the script using the param block in PowerShell.
  • Parameters can be mandatory or optional, and you can set default values for optional parameters.
  • When running a PowerShell script, you specify each parameter’s value after the parameter name, prefixed with a hyphen -.

By following these steps, you can run a PowerShell script from the command line with parameters.

run powershell script from command line with parameters

How to run ps1 file in PowerShell as administrator

To run a .ps1 PowerShell script file as an administrator, follow these steps:

Step 1: Open PowerShell as Administrator

First, you need to open an elevated PowerShell prompt:

  1. Click on the Start button or press the Windows key.
  2. Type PowerShell.
  3. Right-click on the Windows PowerShell and select “Run as administrator”. This will open PowerShell with administrative privileges.

Alternatively, you can use the Win + X keyboard shortcut to open the Power User Menu and select “Windows PowerShell (Admin)”.

Step 2: Change to the Script Directory

Use the cd command to change the current directory to the location of your .ps1 script file:

cd C:\Path\To\Your\Script

Replace C:\Path\To\Your\Script with the actual directory path of your script.

Step 3: Execute the Script

Now, you can run your script by typing .\ followed by the script name:

.\YourScript.ps1

Replace YourScript.ps1 with the name of your script file.

Step 4: Bypass Execution Policy if Necessary

If you encounter an error related to the execution policy, you can bypass it for a single script execution by adding the -ExecutionPolicy Bypass parameter:

PowerShell -ExecutionPolicy Bypass -File .\YourScript.ps1

This command temporarily bypasses the execution policy restriction for the script you are running without changing the policy system-wide.

By following these steps, you should be able to run a .ps1 PowerShell script file as an administrator.

Conclusion

I hope you know how to run a Powershell script from Powershell with parameters. And, also how to run a ps1 file in PowerShell as an administrator.

You may also like: