How to Run a Powershell Script From the Command Line?
How to Run a PowerShell Script from the Command Line
PowerShell, a powerful command-line shell and scripting language, is a favorite tool for system administrators and power users on Windows.
Automation tasks, system management, and accessing a vast range of system functionalities become seamless with PowerShell scripts. In this guide, we will explore how to run a PowerShell script from the command line, providing an essential skill for managing and automating tasks efficiently.
Prerequisites
- PowerShell Installed: Ensure you have PowerShell installed on your system. Windows 10 and later comes with PowerShell pre-installed.
- Script Ready: Have a PowerShell
.ps1
script file prepared and stored on your machine.
Steps to Run a PowerShell Script from the Command Line
1. Open the Command Prompt
Start by opening the Command Prompt. You can do this by pressing Windows + R
, typing cmd
, and hitting Enter
.
2. Change the Execution Policy
Before running scripts, you must ensure the execution policy allows script execution. This can be done by executing the following command:
powershell Set-ExecutionPolicy RemoteSigned
Note: Ensure you have administrative privileges to change the execution policy.
3. Navigate to the Script’s Directory
Use the cd
command to change the directory to where your PowerShell script is located. For example:
cd C:\Path\To\Your\Script
4. Running the PowerShell Script
After navigating to the directory containing the script, run the PowerShell script using the following command:
powershell -File YourScriptName.ps1
Replace YourScriptName.ps1
with the actual script file name.
5. Passing Parameters to the Script
If your script accepts parameters, you can pass them directly in the command like so:
powershell -File YourScriptName.ps1 -Param1 Value1 -Param2 Value2
Additional Resources
To further enhance your PowerShell scripting abilities, check out the following resources:
- Learn how to show dialogs when running PowerShell scripts to improve interactivity.
- Discover techniques on formatting output for PowerShell scripts for better readability and usability.
- Explore handling multiple files in PowerShell scripts efficiently.
- Understand how to remove headers from CSV outputs in PowerShell scripts.
- Learn about using System.Threading.Thread in PowerShell scripts for advanced script control.
Conclusion
Running a PowerShell script from the command line is a straightforward process that unlocks a plethora of automation capabilities for power users. Whether you are deploying enterprise-scale operations or managing your personal tasks, mastering PowerShell scripting is invaluable. Utilize the additional resources to refine your skills and enhance your PowerShell scripting proficiency.