Executing PowerShell Commands Directly from Command Prompt: A No-Script Approach

Dec 05, 2025 · Programming · 9 views · 7.8

Keywords: PowerShell | Command Prompt | AppLocker

Abstract: This article provides an in-depth exploration of executing PowerShell commands directly from the Command Prompt (CMD) without creating .ps1 script files. By analyzing common error cases, it focuses on core techniques using the & operator and proper quotation escaping, with practical examples from the AppLocker module. It covers execution policy configuration, module importing, parameter passing, and multi-command execution, offering actionable solutions for system administrators and automation developers.

Introduction

In Windows system administration, there is often a need to execute PowerShell commands directly from the traditional Command Prompt (CMD) environment, particularly in batch scripts or automated tasks. Many users prefer to avoid creating full PowerShell script files (.ps1), especially when only a few commands are required. However, invoking PowerShell commands directly from CMD frequently leads to issues such as unrecognized commands, no output, or execution failures. Based on real-world Q&A data, this article analyzes the root causes of these problems and presents validated solutions.

Common Issues and Error Analysis

Users attempting to execute PowerShell commands from CMD often encounter the following typical errors:

The underlying causes of these issues stem from the complexity of interaction between CMD and PowerShell, including parameter parsing, string escaping, and module loading differences.

Core Solution: Using the & Operator and Proper Escaping

Through testing, the most reliable solution involves combining the & operator with appropriate quotation escaping. The key syntax is:

powershell -command "& {&'some-command' someParam}"

Here, outer double quotes are used for CMD parameter passing, while the inner & operator ensures the command is executed correctly in the PowerShell context. For example, for AppLocker-related commands:

powershell -command "& {&'Import-Module' AppLocker}"; "& {&'Set-AppLockerPolicy' -XmlPolicy myXmlFilePath.xml}"

This method works effectively because:

  1. Module Importing: Many PowerShell commands (e.g., Get-AppLockerFileInformation) depend on specific modules. Explicitly importing the module via &'Import-Module' AppLocker ensures command availability.
  2. Parameter Passing: Using the & operator to invoke commands and pass parameters avoids incorrect parsing by CMD.
  3. Execution Policy Bypass: Although not explicitly set in the example, adding -ExecutionPolicy Bypass can bypass restrictive execution policies to ensure command execution.

Multi-Command Execution and Advanced Configuration

For scenarios requiring multiple commands, use semicolons to separate them:

powershell -command "& {&'Import-Module' AppLocker}"; "& {&'Get-AppLockerFileInformation' -Directory C:\Path -Recurse -FileType EXE}"

Additionally, it is recommended to include the following parameters for improved reliability and performance:

Complete example:

powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -NonInteractive -command "& {&'Import-Module' AppLocker}"; "& {&'Set-AppLockerPolicy' -XmlPolicy C:\policy.xml}"

Practical Recommendations and Considerations

In practical applications, the following points should be noted:

By applying the methods described in this article, users can efficiently execute PowerShell commands from CMD without relying on script files, simplifying system management and automation processes.

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.