How to Run PowerShell Scripts From the Command Line

How to Run PowerShell Scripts From the Command Line
Image Credit: alvarez/E+/GettyImages

Windows PowerShell is a scripting language you can use in Windows to automate various tasks. You can run PowerShell scripts from the traditional Windows command line or using PowerShell's own command prompt. Make sure to only run PowerShell commands that you've written and debugged or ones from sources you trust, since ones from other sources can do things you may not want on your computer.

Understanding Windows PowerShell

Windows PowerShell is a scripting tool for Microsoft Windows. It allows you to automate various tasks on Windows, including system administration tasks like connecting to networks or backing up files or personal tasks like organizing sets of files or photos. It contains access to much of Microsoft's .NET programming framework, making it a powerful choice for Windows automation.

You can type or copy and paste PowerShell commands into the PowerShell program one by one, which can be useful for testing features or for some simple tasks. But often, you want to organize PowerShell commands into sets of files called scripts that you can edit as needed and run again and again. These files usually have the file extension .ps1, and you can run a PowerShell Script from the command line if you wish or from the PowerShell Integrated Scripting Environment environment, a Microsoft-provided development tool.

PowerShell and Its Alternatives

PowerShell can be used in addition to or instead of traditional Windows batch files, which have extension .bat and have existed in some form for decades. Batch files don't have access to the full range of .NET tools, making PowerShell often more powerful, but they are compatible with older versions of Windows and may be familiar to more programmers and users.

Various Windows programs, such as Microsoft Office tools like Word and Excel, also have their own built-in scripting tools that can be useful for automating tasks within those programs.

Some people prefer to use cross-platform scripting tools that are also commonly found on Linux and Apple macOS, such as the scripting and command line tool bash or scripting languages such as Python and Ruby. Those will generally have to be installed specifically on Windows, however. Programmers can also develop full-fledged Windows applications using development environments like Microsoft Visual Studio, but this is often more work than is needed for basic automation work.

Run a PowerShell Script

You can create a PowerShell script in the PowerShell ISE, found by typing "PowerShell Integrated Scripting Environment" into the search box on the task bar or in the Start Menu, or in any text editor. That includes Notepad, which is included with Windows. Make sure to save the script with the extension .ps1.

Then, to execute the PowerShell script from the command line, launch the Windows command line by typing "cmd" into the search box and clicking the result. Type the full path of the PowerShell script, such as "C:\Example\example_script.ps1" and press enter. Alternatively, you can navigate to the script file in the PowerShell ISE. Use the "File" menu's "Open" option to navigate to the script and open it, and then click "File" and "Run" to run the script.

If you receive an error saying you don't have permission to run the script, launch PowerShell to change your settings. To do so, search for "PowerShell" in the search box and click the result. Then, type "Unblock-File -Path" followed by a space and the full path of the PowerShell script. You can also change your settings to allow any PowerShell script to run, but make sure you understand the security ramifications of doing so before making this decision.

references