Run Powershell command from cmd with parameters

2 min read 27-10-2024
Run Powershell command from cmd with parameters

When working on Windows, you may find the need to execute PowerShell commands directly from the Command Prompt (CMD). This is useful for system administrators and developers looking to streamline workflows. In this article, we'll explore how to run PowerShell commands from CMD, including passing parameters for a more dynamic operation.

Understanding the Problem

Original Code:

powershell -command "Get-Process -Name notepad"

In the above example, we’re running a simple PowerShell command from CMD to retrieve details about the Notepad process. However, if you want to pass additional parameters to your PowerShell command, the syntax can get a little tricky. For instance, the current command lacks flexibility in changing the parameters.

Corrected and Enhanced Command Syntax

To execute a PowerShell command with parameters from CMD, you can use the following syntax:

powershell -Command "Get-Process -Name <ProcessName>"

To utilize this command effectively, replace <ProcessName> with the desired name of the process you want to query.

Example of Running PowerShell with Parameters

Let’s say we want to run the Get-Process command and check for multiple processes, such as Notepad and Calculator. The command will look like this:

powershell -Command "Get-Process -Name Notepad, Calculator"

Analysis and Explanation

  1. Why Use CMD to Run PowerShell?
    Sometimes, you may find yourself in a situation where you prefer the simplicity and familiarity of CMD over PowerShell. CMD can also be easier to automate in certain scripts or batch files.

  2. Using Parameters
    You can pass parameters to your PowerShell commands to make them more versatile. For example, you could define a variable in CMD to represent the process name:

    set ProcessName=notepad
    powershell -Command "Get-Process -Name %ProcessName%"
    
  3. Chaining Commands
    You can even chain multiple PowerShell commands together. For example, if you want to get a process and then stop it, the command would look like this:

    powershell -Command "Get-Process -Name Notepad; Stop-Process -Name Notepad"
    

Practical Example

If you're a developer and need to regularly monitor the performance of a specific application, you can create a simple batch file:

@echo off
set ProcessName=notepad
powershell -Command "Get-Process -Name %ProcessName% | Select-Object Id, ProcessName, CPU"

This will output the CPU usage of Notepad every time you run the batch file, providing quick insights without needing to open PowerShell separately.

Conclusion

Running PowerShell commands from CMD with parameters is a powerful method for users wanting to leverage PowerShell's capabilities without leaving the Command Prompt environment. By utilizing the correct syntax and parameterization techniques, you can create dynamic and efficient scripts.

Additional Resources

This approach can greatly improve your command-line proficiency and streamline your automation tasks. Happy scripting!