Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Is a .ps1 script being run from PShell or PowerShell 5.1?

Aug
2,799
144
This is a simple test to determine if a .ps1 script is being executed via PShell or PowerShell 5.1

Code:
$IsPshell = $true

try {
    $null = [TakeCommand.PowerShellHost]::ExpandValue('%_cwd')
}
catch {
    $IsPshell = $false
}

if ($IsPshell) {
    # PSHELL-only callbacks
    $cwd = [TakeCommand.PowerShellHost]::ExpandValue('%_cwd')
    Write-Host "PSHELL cwd: $cwd"
} else {
    Write-Host "Running under PowerShell 5.1"
}

Joe
 
Back
Top