2025, Dec 05 03:00
Why Get-Process CommandLine is empty when run from Python, and how calling PowerShell 7 (pwsh.exe) fixes it
Get-Process CommandLine prints blank from Python? The issue is Windows PowerShell. Call PowerShell 7 (pwsh.exe) to get real output and reliable automation.
When you call PowerShell from Python, everything looks straightforward until one subtle detail changes: the PowerShell engine. A command that renders cleanly in an interactive console can return empty or misleading output when executed programmatically, especially if you rely on properties that exist only in newer PowerShell versions. A common trap is querying the CommandLine property from process objects.
Reproducing the issue
The following Python snippet executes a PowerShell pipeline and returns its formatted table. It behaves as expected for a generic table:
import subprocess
def collect_ps_table(cmd):
try:
outcome = subprocess.run(
["powershell", "-Command", cmd],
capture_output=True,
text=True,
check=True,
)
return outcome.stdout
except subprocess.CalledProcessError as err:
print(f"Error executing PowerShell command: {err}")
return None
ps_cmd = "Get-Process python | Format-Table -AutoSize"
tabular = collect_ps_table(ps_cmd)
if tabular:
print(tabular)
Switching the pipeline to request the CommandLine property suddenly yields no meaningful output:
ps_cmd = "Get-Process python | Format-Table CommandLine -AutoSize"
In a PowerShell terminal you might see results, but from Python the output appears blank or not “correct.”
What’s actually going on
The behavior doesn’t originate in Python. It’s about which PowerShell you’re invoking. The CommandLine property on objects returned by Get-Process is available in PowerShell (Core) 7. In Windows PowerShell, Get-Process doesn’t expose that property, so formatting a non-existent property results in empty-looking output. As one succinct summary puts it:
The problem isn’t related to Python, but to the specifics of the PowerShell command being executed.
This also explains why the exact same command may “work” on one machine and not on another: different PowerShell versions are being called. On a fresh system, you might find that CommandLine simply isn’t there unless you use the newer engine. If you’re tied to Windows PowerShell and need command lines, an alternative is to query Win32_Process: Get-CimInstance -ClassName Win32_Process | Format-Table CommandLine.
The fix: target PowerShell 7 explicitly
Point your subprocess call at PowerShell 7’s pwsh.exe. With that change, querying CommandLine works as expected:
import subprocess
def collect_ps_table(cmd):
"""Executes a PowerShell command and returns the formatted table output as a string."""
try:
outcome = subprocess.run(
[r"C:\Program Files\PowerShell\7\pwsh.exe", "-Command", cmd],
capture_output=True,
text=True,
check=True,
)
return outcome.stdout
except subprocess.CalledProcessError as err:
print(f"Error executing PowerShell command: {err}")
return None
ps_cmd = "Get-Process python | Format-Table CommandLine -AutoSize"
tabular = collect_ps_table(ps_cmd)
if tabular:
print(tabular)
This aligns your Python-run command with what you see when using PowerShell (Core) 7 interactively.
Why this matters
Automation often runs in environments where multiple PowerShell versions coexist. If your code relies on properties or behavior introduced in PowerShell 7, you must invoke pwsh.exe explicitly. Otherwise, your program may silently call Windows PowerShell and return empty output, especially when formatting properties that don’t exist there. If you still get unexpected results, examining stderr can surface helpful PowerShell error details.
Takeaways
Be deliberate about the PowerShell engine you launch from Python. If you need CommandLine from Get-Process, run through PowerShell (Core) 7. If you’re constrained to Windows PowerShell, use commands that expose the data you need, such as querying Win32_Process. When diagnosing odd output, check error streams to confirm whether the issue lies in the command itself rather than in Python’s subprocess handling.