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:
- The
&operator is used to invoke scripts or commands - Script path is wrapped in single quotes to ensure proper handling of spaces in the path
- Parameter names and values are passed to the script as separate arguments
- The
-noexitparameter keeps the PowerShell session from exiting after script execution, facilitating debugging
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:
- Clearer and more intuitive syntax
- Automatic parameter handling without manual separation
- Support for positional parameters passed to the script in order
- Reduced complexity of quote nesting
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:
powershell.exe- Interactive command-line interfacepowershell_ise.exe- Interactive scripting environment
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:
- First test without the
-noexitparameter to observe error output - Use
-ExecutionPolicy Bypassto bypass execution policy restrictions - Add detailed logging output and error capture within the script
- Verify parameter reception and processing logic inside the script
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:
- Explicitly specify the target PowerShell version
- Add version checking logic at the beginning of scripts
- Test compatibility across different Windows versions and PowerShell versions
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.