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.
- Open PowerShell as an administrator by searching for “PowerShell” in the Start menu, right-clicking on the Windows PowerShell, and selecting “Run as administrator”.
- Set the execution policy with the following command:
Set-ExecutionPolicy UnrestrictedThis 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 useRemoteSignedwhich allows running scripts that are created on your local machine or signed by a trusted publisher.
Step 2: Write Your PowerShell Script
- Open your text editor of choice.
- Write your PowerShell script. For example:
Write-Host "Hello, World!" - Save the file with a
.ps1extension, for example,MyScript.ps1.
Step 3: Run the PowerShell Script from CMD
- Open the Command Prompt by typing
cmdin the Start menu. - Navigate to the directory where your script is saved using the
cdcommand. For example:cd C:\Path\To\Your\Script - 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 Bypassparameter 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
- Open a text editor and create a new PowerShell script file with a
.ps1extension. - Define parameters at the beginning of your script using the
paramkeyword. For example:param ( [string]$Name, [int]$Age ) Write-Host "Hello, $Name! You are $Age years old." - Save the script, for example, as
MyScriptWithParams.ps1.
Step 2: Run the Script from the Command Line with Parameters
- Open Command Prompt (CMD).
- Navigate to the directory where your script is located using the
cdcommand. - 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 30In this example, “John” is passed to the$Nameparameter, and30is passed to the$Ageparameter.
Understanding Parameters in PowerShell
- Parameters are defined in the script using the
paramblock 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.

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:
- Click on the Start button or press the
Windowskey. - Type
PowerShell. - 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\ScriptReplace 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.ps1Replace 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.ps1This 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:
- How to Run PowerShell Script in PowerShell ISE
- How to Check PowerShell Version?
- PowerShell Naming Conventions
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.