1. Why Use PowerShell To Call External Commands?
PowerShell is a powerful shell and scripting language built into Windows, widely used for system administration and automation. While typing commands directly into a PowerShell terminal is convenient for quick tasks, placing these commands inside a script is far more efficient for repeatable actions.
Here’s why scripting is better:
– Automates repetitive tasks
– Saves time and minimizes manual errors
– Makes commands more organized and maintainable
– Enhances clarity with inline comments
– Enables easier debugging and testing
Using external commands in scripts—such as launching Notepad or listing running processes—is a simple way to get started.
2. Method 1: Direct Command Execution Inside a Script
The simplest way to run a Windows command inside a PowerShell script is to type it just as you would in a terminal.
notepad "C:\Users\Example\Desktop\test.txt" tasklist
These lines launch Notepad with a specified file and then list all currently running processes.
To try this:
- Open Visual Studio Code
- Create a new `.ps1` file (e.g., `test.ps1`)
- Write the above commands
- Run the script using the non-debug mode button in VS Code
This approach is easy and perfect for beginners.
3. Method 2: Use Start-Process For More Control
If you want more flexibility—like passing arguments, running as administrator, or hiding windows—PowerShell provides the `Start-Process` cmdlet.
Example:
Start-Process "notepad.exe" -ArgumentList "C:\Users\Example\Desktop\test.txt"
This line does exactly what the previous method does but gives you more room to expand.
To run the process as administrator:
Start-Process "notepad.exe" -ArgumentList "C:\Users\Example\Desktop\test.txt" -Verb RunAs
This is helpful when dealing with scripts that require elevated privileges to execute certain operations.
4. Method 3: Combine Commands Into A Full Automation Flow
You can chain commands together in a script to form a complete automation pipeline. For instance:
tasklist > C:\temp\process.txt Start-Process "notepad.exe" -ArgumentList "C:\temp\process.txt"
The above script first saves the list of active processes into a text file, then opens it in Notepad. This is a practical example of automating a system monitoring task.
Such combinations make your scripts versatile and powerful—ideal for real-world use in IT workflows or even personal productivity hacks.
5. Conclusion: Enhance Efficiency With External Commands in Scripts
By integrating external Windows commands into your PowerShell scripts, you can significantly improve automation, consistency, and ease of maintenance. Whether you use simple direct commands or the more advanced `Start-Process` method, PowerShell gives you the tools to create robust and reusable scripts. Start experimenting with your own scripts today and bring automation to your daily workflow!
6. Demo Video
You can watch the following demo video by select the subtitle to your preferred subtitle language.