Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - how to determine programmatically whether StrictMode is set?

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?

like image 262
wfr Avatar asked Apr 28 '26 19:04

wfr


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
like image 135
Theo Avatar answered May 01 '26 09:05

Theo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!