Skip to content

Synchronous Execution

Chamoswor edited this page Oct 19, 2025 · 4 revisions

Single command

with Shell() as sh:
    r = sh.run("Get-Process | Select-Object -First 1 Name")
    assert r.success

Batch

with Shell() as sh:
    rs = sh.run(["Get-Date", "Get-Random", "Get-Location"])  # List[ExecutionResult]
    for r in rs:
        if not r.success:
            print("failed:", r.err)

Raising on error

with Shell() as sh:
    r = sh.run("throw 'boom'", raise_on_error=False)  # no Python exception
    try:
        sh.run("throw 'boom'", raise_on_error=True)   # raises ExecutionError
    except Exception as e:
        print(e)

Clone this wiki locally