Complete Guide to Executing Parameterized PowerShell Scripts in CMD

Nov 19, 2025 · Programming · 13 views · 7.8

Keywords: PowerShell | CMD | Parameter_Passing | Script_Execution | Windows_Automation

Abstract: This article provides an in-depth exploration of correctly executing PowerShell scripts with parameters in Windows Command Prompt. Through analysis of common error cases, it thoroughly examines proper parameter passing syntax, compares different approaches using the & operator and -file parameter, and offers comprehensive code examples with best practice recommendations. The content also covers fundamental knowledge of PowerShell execution environments, helping readers master the technical details of cross-script engine invocation.

Problem Background and Common Errors

In Windows system administration, there is often a need to invoke PowerShell scripts from the Command Prompt to accomplish complex automation tasks. However, when scripts require input parameters, many developers encounter execution failures.

A typical error example is as follows:

powershell.exe -noexit "& 'D:\Work\SQLExecutor.ps1 -gettedServerName "MY-PC" '"

This code produces the error message: "The term 'D:\Work\SQLExecutor.ps1 -gettedServerName "MY-PC" ' is not recognized as the name of a cmdlet, function". The root cause is the confusion between parameters and script path, where the PowerShell interpreter treats the entire string as a command name rather than separate script path and parameters.

Correct Parameter Passing Methods

Method 1: Proper Syntax Using & Operator

To correctly pass parameters, the script path and parameters must be clearly separated:

powershell.exe -noexit "& 'D:\Work\SQLExecutor.ps1' -gettedServerName 'MY-PC'"

The core principles of this syntax are:

Method 2: Simplified Syntax Using -file Parameter

PowerShell provides a more concise -file parameter for script execution:

powershell.exe -noexit -file "D:\Work\SQLExecutor.ps1" "MY-PC"

The advantages of this method include:

In-depth Analysis of PowerShell Execution Environment

According to PowerShell official documentation, Windows PowerShell is a scripting engine embedded into multiple hosts. The most common hosts include:

Starting from PowerShell 6, the binary file names changed to pwsh.exe (Windows) and pwsh (macOS and Linux), built on .NET Core with cross-platform support.

Code Examples and Best Practices

Basic Script Invocation Pattern

For simple script calls without parameters:

powershell.exe -noexit "& 'C:\Data\ScheduledScripts\ShutdownVM.ps1'"

Multiple Parameter Passing Example

When a script requires multiple parameters:

powershell.exe -file "D:\Work\MyScript.ps1" "param1" "param2" "param3"

Advanced Usage of Named Parameters

For complex scenarios requiring explicit parameter names:

powershell.exe -noexit "& 'C:\Scripts\Config.ps1' -ServerName 'SRV01' -Port 8080 -EnableSSL $true"

Error Handling and Debugging Techniques

When executing parameterized PowerShell scripts, the following debugging strategies are recommended:

Cross-Version Compatibility Considerations

It's important to note that different versions of PowerShell may have subtle differences in parameter handling and syntax. For production environments, it is advised to:

Conclusion

Correctly executing parameterized PowerShell scripts in CMD requires understanding the fundamental principles of parameter passing. By clearly separating script paths and parameters, or using the dedicated -file parameter, common syntax errors can be avoided. Mastering these technical details is crucial for Windows system administration and automation script development, significantly improving work efficiency and script reliability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.