Windows commands can’t easily access PowerShell’s advanced scripting and automation features, but you can embed it directly using the `$powershell` alias or command-line syntax. Instead of opening PowerShell manually, run it inline via CMD, PowerShell itself, or even remotely over SSH—this saves time and integrates seamlessly with batch files or scripts. The alias works in any Windows command prompt or PowerShell session, and the underlying command (`powershell.exe`) supports full scripting, including pipelines and cmdlets, without a separate window. No extra tools or admin rights are required beyond basic permissions for the target folder or process.

What You'll Need

  • Windows 10/11 with PowerShell 5.1 or later installed (it comes pre-installed)
  • A command prompt (CMD) or PowerShell session where you want to execute PowerShell scripts
  • Basic familiarity with Windows paths, file permissions, or the script’s expected output (if applicable)

Step-by-Step

  1. Method 1: Use the `$powershell` Alias (Quick Execution)

    In any CMD or PowerShell prompt, type the alias directly: $powershell Get-ChildItem "C:\Temp"

    This executes the command as a one-liner and returns results immediately. The alias is case-sensitive but more convenient for simple queries. Note: The alias only works in PowerShell’s context; to use it in CMD, you must enable it by running: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force first (requires admin rights).

  2. Method 2: Use `powershell.exe` with Command-Line Arguments (Full Control)

    Run PowerShell as a standalone executable with a script or command: powershell.exe -Command "Get-Process | Where-Object { $_.Name -like 'chrome*' }"

    Key flags:

    • -Command: Passes the script as a string (supports pipelines and cmdlets).
    • -NoProfile: Skips loading profiles (useful for remote sessions).
    • -ExecutionPolicy Bypass: Ignores policy restrictions (add to the command).

  3. Method 3: Pipe PowerShell into CMD (Integration)

    To run PowerShell commands and feed results into CMD: powershell.exe -Command "Get-Date" | Out-Host

    Or redirect output to a file: powershell.exe -Command "Get-Service | Where-Object {$_.Status -eq 'Running'}" > services.txt

  4. Method 4: Run a Script File (Step-by-Step)

    Create a script (e.g., `test.ps1`) and execute it inline: powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\test.ps1"

    For remote scripts, use: powershell.exe -ExecutionPolicy Bypass -Command "& { .\remote_script.ps1 }"

  5. Method 5: SSH/Remote Execution (Advanced)

    If you’re using PuTTY/SSH, run PowerShell remotely: powershell.exe -Command "Get-WmiObject Win32_OperatingSystem"

    On Windows 10/11, you can also use:

Common Mistakes

  • Execution Policy Errors

    If you see `Execution Policy Restriction`, add `-ExecutionPolicy Bypass` to your command. To reset the policy globally (requires admin), run: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force Warning: Changing this policy affects all users; consult IT before modifying.

  • Missing `-Command` Flag

    If PowerShell fails silently, ensure you’re passing a command string (not a script file). Example works: powershell.exe -Command "Write-Host 'Hello'" powershell.exe -File script.ps1 only works for files.

  • Permissions Denied

    If the command fails with "Access is denied," check:

    • The target script/file path exists.
    • The user has read/execute permissions.
    • For remote machines, ensure firewall allows SSH/PowerShell.

  • Output Redirection Issues

    If `> file.txt` fails, use explicit paths or check disk space: powershell.exe -Command "Get-WmiObject Win32_Processor | Out-File "C:\Temp\output.txt""

Tips

  • Mix Methods for Efficiency

    Combine `$powershell` in CMD for quick queries and `powershell.exe` for scripts. Example: echo "Running:"; $powershell Get-Date

  • Use `-NoProfile` for Remote Sessions

    Avoid loading profiles in remote execution to prevent conflicts: powershell.exe -NoProfile -Command "Write-Host 'Running remotely'"

  • Debug with `-Verbose`

    Enable detailed logging for troubleshooting: powershell.exe -Command "Get-Service | Format-Table -Property Name, Status -AutoSize" -Verbose

  • Save Command History

    Redirect output to a file for later use: powershell.exe -Command "Get-ChildItem *log*" > logs.txt

  • Test Locally First

    Always validate your script locally before running remotely. Example: powershell.exe -Command "(Get-Date).ToString('yyyy-MM-dd')" | Out-Host

Troubleshooting

  1. Command Not Recognized

    If `$powershell` or `powershell.exe` fails, verify:

    • PowerShell is installed (check via `powershell.exe -Version`).
    • No typos in the command.
    • You’re not in a restricted directory (e.g., a script or child folder).

    Reset the prompt: Set-PSReadLineOption -ConfirmSwitch $false to test syntax highlighting.

  2. Script Doesn’t Execute

    If `powershell.exe -File script.ps1` fails:

    • Check the script path is correct.
    • Ensure `.ps1` extension is allowed (use `.` for inline execution).
    • Run PowerShell as admin if the script requires elevated permissions.

  3. Network Issues (Remote Execution)

    For SSH/PowerShell remoting:

    • Test connectivity: `Test-NetConnection REMOTE_MACHINE -Port 5986` (SSH).
    • Enable WinRM on remote machine if using `Invoke-Command`: Enable-PSRemoting -Force
    • Check firewall rules allow ports 5985/5986 (SSH) or 5986 (WinRM).

  4. Output Corrupted

    If results appear garbled, try: powershell.exe -Command "Get-Service | Format-List *" or redirect to a text file: powershell.exe -Command "Get-Process | Out-File "C:\Temp\processes.txt"

  5. Performance Lag

    For slow responses:

    • Use `-NoProfile` for remote sessions.
    • Avoid complex scripts in single commands (split into multiple steps).
    • Upgrade PowerShell to 7.x for better performance.

Frequently Asked Questions

1. Can I use `$powershell` in CMD without enabling the policy?

No. The alias requires the execution policy to be set to `RemoteSigned` (or `Bypass`) via `Set-ExecutionPolicy`. To use it in CMD without admin rights, reset the policy: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser before opening CMD.

2. What if I get a "Script cannot be loaded" error?

The error typically means the script isn’t in the current profile or path. Solutions:

  • Save the script in the current directory and run: powershell.exe -Command "& { . script.ps1 }"
  • Use full paths: powershell.exe -File "C:\Path\script.ps1"
  • Check `$env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1` exists.

3. Can I run PowerShell scripts from a Windows batch file?

Yes. Use: powershell.exe -ExecutionPolicy Bypass -File "script.ps1" or inline commands: powershell.exe -Command "Get-Service | Where-Object {$_.Status -eq 'Running'}" Always escape special characters in batch files (e.g., `& { cmd | powershell }`).

4. Why does `powershell.exe` take so long to start?

PowerShell’s startup time depends on:

  • Profile loading (if enabled).
  • Remote execution (network delays).
  • Execution policy checks.
-NoProfile or -ExecutionPolicy Bypass can speed it up.

5. Can I pipeline PowerShell into CMD?

Yes. Example: powershell.exe -Command "Get-Date" | clip to copy output to clipboard, or: powershell.exe -Command "Get-Service" | Out-Host to display results in CMD.

6. What’s the difference between `$powershell` and `powershell.exe`?

The alias (`$powershell`) is a shortcut for entering PowerShell interactively. The executable (`powershell.exe`) runs scripts or commands directly without opening a window, making it ideal for batch files or remote execution. Use `$powershell` for quick queries, `powershell.exe` for automation.