Skip to content

Terminate Powershell script or session#

I always asked myself how to terminate a Powershell script or session, each time I needed to do some tests by myself and also searched on Google. But I could never remember it. So I would like to take this post to note it down, the next time I need to terminate, just need to come back to here.

Terminate the current Powershell script#

Way 1 - Exit#

Exit without exit code#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        exit
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> .\test.ps1
beginScript
beginFunction
1

5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: True
Last exit code: 0

5.1>

Exit with code 0#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        exit 0
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> .\test.ps1
beginScript
beginFunction
1

5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: True
Last exit code: 0

5.1>

Exit with code 1#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        exit 1
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> .\test.ps1
beginScript
beginFunction
1

5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: False
Last exit code: 0

5.1>

Way 2 - Break#

Break with an UnknownLabel terminates the script directly#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        break foobar
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> .\test.ps1
beginScript
beginFunction
1

5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: True
Last exit code: 0

5.1>

But it terminates also the caller script#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        break foobar
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> Get-Content .\call-test.ps1
Write-Output 'Call test.ps1'
./test.ps1
Write-Output 'End call test.ps'
Write-Output "call-test.ps1: Last execution status: $?"
Write-Output "call-test.ps1: Last exit code: $LASTEXITCODE"

5.1> .\call-test.ps1
Call test.ps1
beginScript
beginFunction
1

5.1>

Warning

Never use break UnknownLabel to terminate the script. Break does't raise error, the caller script cannot catch its output.

Terminate the current Powershell session#

Way 1 - System.Environment.Exit#

https://docs.microsoft.com/en-us/dotnet/api/system.environment.exit

Environment.Exit with code 0 and started by powershell.exe#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        [Environment]::Exit(0)
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> powershell -noprofile .\test.ps1
beginScript
beginFunction
1

5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: True
Last exit code: 0

5.1>

Environment.Exit with code 1 and started by powershell.exe#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        [Environment]::Exit(1)
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> powershell -noprofile .\test.ps1
beginScript
beginFunction
1

5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: False
Last exit code: 0

5.1>

Environment.Exit with code 0 and started by Start-Process#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        [Environment]::Exit(0)
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> Start-Process .\test.ps1


5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: True
Last exit code: 0

5.1>

Environment.Exit with code 1 and started by Start-Process#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        [Environment]::Exit(1)
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> Start-Process .\test.ps1


5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: True
Last exit code: 0

5.1>

Way 2 - Stop-Process#

Powershell has an automatic variable called $PID which refers to the process ID that is hosting the current PowerShell session.

Stop-Process started by powershell.exe#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        Write-Output "Kill process $PID"
        Stop-Process $PID
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> powershell -NoProfile .\test.ps1
beginScript
beginFunction
1
Kill process 12348

5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: False
Last exit code: 0

5.1>

Stop-Process started by Start-Process#

5.1> Get-Content .\test.ps1
function foo {
    Write-Output beginFunction
    1..3 | % {
        Write-Output $_
        Write-Output "Kill process $PID"
        Stop-Process $PID
    }
    Write-Output endFunction
}

Write-Output beginScript
foo
Write-Output endScript

5.1> Start-Process .\test.ps1


5.1> Write-Host "Last execution status: $?" ; Write-Host "Last exit code: $LASTEXITCODE"
Last execution status: True
Last exit code: 0

5.1>

Conclusion#

GoalTerminate MethodLast execution status: $?Last exit code: $LASTEXITCODEComment
Terminate ScriptexitTrue0
Terminate Scriptexit 0True0
Terminate Scriptexit 1False0
Terminate Scriptbreak UnknownLabelTrue0Never use it
Terminate Process[Environment]::Exit(0) started by powershell.exeTrue0
Terminate Process[Environment]::Exit(1) started by powershell.exeFalse0
Terminate Process[Environment]::Exit(0) started by Start-ProcessTrue0
Terminate Process[Environment]::Exit(1) started by Start-ProcessTrue0
Terminate ProcessStop-Process started by powershell.exeFalse0
Terminate ProcessStop-Process started by Start-ProcessTrue0

Comments