PowerShell - how to determine programmatically whether StrictMode is set?

Viewed 762

I know that I can override in a script or function the StrictMode setting inherited from a higher scope. But how can I find out in a script or function what the inherited setting is?

1 Answers

Perhaps a small function can help:

function Get-StrictMode {
    # returns the currently set StrictMode version 1, 2, 3
    # or 0 if StrictMode is off.
    try { $xyz = @(1); $null = ($null -eq $xyz[2])}
    catch { return 3 }

    try { "Not-a-Date".Year }
    catch { return 2 }

    try { $null = ($undefined -gt 1) }
    catch { return 1 }

    return 0
}

Get-StrictMode
Related