PowerShell is a powerful scripting language and shell environment for managing Windows systems. With PowerShell, you can automate administrative tasks, configure systems, and execute complex operations using easy-to-read syntax.
One of the great aspects of PowerShell is the ability to run scripts from the command line. This allows you to execute PowerShell code without entering an interactive shell session. Whether you want to run simple one-liners or complex scripts, the command line provides a quick way to leverage PowerShell‘s capabilities.
In this comprehensive guide, you‘ll learn multiple methods for running PowerShell scripts from the command line on Windows.
Overview of Running PowerShell Scripts
Here is a quick overview of the options covered in this guide for executing PowerShell scripts from the command line:
-
PowerShell.exe – Use the powershell.exe executable with command line parameters to launch scripts.
-
CMD.exe – Call PowerShell scripts from within CMD.exe command prompts.
-
Scheduled Tasks – Schedule PowerShell scripts to run automatically using the task scheduler.
-
Remote Execution – Remotely execute PowerShell scripts on other systems via WS-Man (WinRM).
Let‘s explore each of these techniques in more detail.
Using PowerShell.exe to Run Scripts
The most straightforward way to execute a PowerShell script from the command line is by calling the powershell.exe executable with the appropriate parameters.
Here is the basic syntax to launch a PowerShell script file using powershell.exe:
powershell.exe -File <path to ps1 script> <optional parameters>
For example, to run a script called my-script.ps1 located at C:\Scripts, you would use:
powershell.exe -File C:\Scripts\my-script.ps1
This launches powershell.exe, passes the script file path, executes the code, and then exits back out to the command prompt automatically on completion.
You can also provide additional parameters after specifying the script file which will be passed to the script runtime.
PowerShell.exe Command Line Options
Here are some other helpful parameters you can use with powershell.exe to control script execution behavior from the command line:
| Parameter | Description |
|---|---|
| -File | Specify path to a script file to execute |
| -Command | Directly execute PowerShell commands instead of launching the shell |
| -ExecutionPolicy | Set execution policy for the session |
| -InputFormat | Specify input format as Text (default) or XML |
| -Mta | Start session in multi-threaded apartment mode |
| -NoExit | Keep the shell session open after completing the script |
| -NoLogo | Hide the copyright banner at launch |
| -NonInteractive | Disable interactive prompts, ideal for automated runs |
| -NoProfile | Launch session without running PowerShell profiles |
| -OutputFormat | Generate output as Text (default) or XML |
| -Sta | Use single threaded apartment mode |
| -WindowStyle | Control window presence with Normal, Minimized, Maximized, or Hidden |
Using parameters provides more control over how PowerShell.exe handles your scripts.
Calling PowerShell Scripts from CMD.exe
You can also execute PowerShell script files directly from CMD.exe command prompts.
This allows you to run PowerShell code from within batch files or other CMD commands.
To call a PowerShell script from CMD.exe, use this syntax:
powershell -File <path to ps1 script>
For example:
powershell -File C:\Scripts\start-server.ps1
This will launch the specified PowerShell script and return control back to CMD after completion.
You can pass additional parameters from CMD to the PowerShell runtime using the -Command parameter instead. For example:
powershell -Command "& {Get-Service}"
This executes the Get-Service cmdlet within the PowerShell environment and returns to CMD.
Handling Output from CMD
To capture output from a PowerShell script within CMD, append redirection operators:
powershell -File C:\Scripts\get-processes.ps1 > process-list.txt
This runs the PowerShell script and saves its console output to process-list.txt.
You can also pipe PowerShell commands directly to CMD utilities like FIND or MORE for filtering or paged output:
powershell -Command "& {Get-Service}" | FIND " bits"
So CMD provides a handy way to integrate with PowerShell capabilities.
Scheduling PowerShell Scripts Using Tasks
Another useful technique is running PowerShell scripts automatically using the built-in Task Scheduler. This allows scripts to execute on a recurring schedule.
For example, you could run a script that checks log sizes every day, or run a configuration script whenever a system boots up.
Creating a Basic Scheduled Task
Here are the steps to create a simple scheduled task that runs a PowerShell script:
- Open Task Scheduler
- Click Create Task
- Give the task a name and description on the General tab
- Under Triggers, click New to set up your desired schedule
- On the Actions tab, click New and configure a "Start a program" action
- Specify the Program/script path as powershell.exe
- Add the -File parameter with path to your .ps1 script
- Specify any additional parameters needed
- Click OK to save the new task
Once configured, your PowerShell script will now run automatically per the defined trigger schedule.
Configuring Task Details
You can set up quite complex schedules, conditions, and settings for tasks in Task Scheduler that give you fine-grained control over automated PowerShell scripts.
For example, you can:
- Run scripts at system startup or logon
- Set repeat intervals by day, hour, minutes
- Delay task startup conditions
- Add conditional logic and advanced triggers
- Configure security and credential settings
- Generate operational logs for monitoring
Check the Task Scheduler documentation for all the configuration options available for automating PowerShell scripts.
Running Scripts on Remote Systems via WS-Man
PowerShell also makes it easy to execute scripts on remote Windows computers using WS-Man based remoting.
This uses the underlying Windows Remote Management (WinRM) infrastructure for running commands over the network on remote hosts.
Here is an example PowerShell one-liner that uses Invoke-Command to call a script on a remote system:
Invoke-Command -ComputerName SERVER01 -FilePath C:\Scripts\Get-DiskSpace.ps1
This runs Get-DiskSpace.ps1 on the SERVER01 remote host over the network.
You can execute scripts on multiple systems by passing a comma-separated list of computer names. Any output is retrieved and displayed locally.
Configuring WinRM for PowerShell Remoting
To enable PowerShell remote capability, WinRM needs to be properly configured on the target systems:
- Run
Enable-PSRemotingon hosts to ensure remoting is activated - May need to check firewall rules permitting WS-Man RPC traffic on TCP port 5985
- Consider enabling SSL for secure communications using port 5986
There are also more advanced remoting configuration options available too like custom authentication, proxies, just enough administration, etc.
But once WinRM is ready, you get robust command execution with integrated output retrieval across Windows networks.
Final Thoughts
As you can see, PowerShell offers tremendous flexibility for running script code from the command line. Whether you need to perform one-off executions or recurring automation, there are options for integrating external logic and custom operations.
The command line techniques explored provide lightweight invocation without needing interactive shell sessions. And leveraging scheduled tasks or WinRM allows for extensive orchestration to suit various operational requirements.
With a bit of creativity, you can utilize these PowerShell run techniques to vastly enhance administrative capabilities as well as implement sophisticated process automation across the Windows ecosystem.
So unlock the possibilities and see where PowerShell scripting can take you!


