A Python wrapper around subprocess to run PowerShell commands and retrieve
their outputs.
Tested on Windows only. The package may work on Linux with PowerShell (
pwsh) installed, but this is not tested.
pip install powershell-commands-wrapperCommands are expressed as a list of strings — the first element is the cmdlet name, followed by its arguments.
import powershell
stdout = powershell.run(["Write-Host", "hello world"])
# stdout = "hello world"Pass as_json=True to retrieve the output as a JSON object:
import powershell
disks = powershell.run(["Get-Disk"], as_json=True)
# disks = [{"Size": 10737418240, "DiskNumber": 1, "PartitionStyle": "MBR", ...}]By default a powershell.PowerShellError is raised when the exit code is
non-zero. Set raise_on_error=False to suppress this:
try:
powershell.run(["Some-Command"])
except powershell.PowerShellError as e:
print(e.result.returncode)
print(e.result.stderr)
# Or ignore errors entirely
powershell.run(["Some-Command"], raise_on_error=False)This package is maintained by Nuabee, a company that helps businesses improve their IT resiliency.