I ran into a bug in the 3rd and 4th version of powershell (in the 2nd version everything works)

$newvar=if (0) {55}; switch($newvar) { "r"{1}; default{2}} 

Adding else makes the code workable (on version 3.4)

 $newvar=if (0) {55} else {$null}; switch($newvar) { "r"{1}; default{2}} 

In the shortened form, switch($(if (0) {55})) { default{2}} does not work on any version. But if you add something else it will work everywhere:

 switch($(if (0) {55} else {$null})) { default{2}} 

Can someone tell me where to find the full list of powershell bugs / features?

    2 answers 2

    To be honest, for me this is the expected behavior. If the command has written zero elements to the output stream, then the result of the command execution is not $null , but [System.Management.Automation.Internal.AutomationNull]::Value . This special value is perceived as an empty set, and not as one value $null . A cycle over an empty set (and a switch in PowerShell is just a cycle) will give zero iterations, which you actually observe. And in PowerShell v2 there is a bug / feature: when saving AutomationNull to a variable, AutomationNull converted to the usual $null .

    As for the list of bugs, the official source is Microsoft Connect .

    • Thanks for the answer. However, with the interpretation of disagree. The switch($anyvalue) { default {1} } construction should, in any case, issue either an exception or 1. If this construction does not do this, then this is not a feature, but just a bug. Actually the switch works correctly (Even with a previously unused variable), if there is no assignment of the result if in front of it without an else. In addition, if the value of the variable after the truncated if becomes [System.Management.Automation.Internal.AutomationNull]::Value then why if it works fine with it $any= if (0){1}; if ($any -eq $null ) {1} $any= if (0){1}; if ($any -eq $null ) {1} , transform - Boris Telesnin
    • I repeat what PetSerAl said: switch is a loop operator. Including from the point of view of the programmer. Look at least technet.microsoft.com/en-us/library/ff730937.aspx , section "Using Arrays with the Switch Statement" - Pavel Mayorov
    • Thank. That's clearer. Really you are right. I inattentively read the answer. - Boris Telesnin

    Here is a great selection of oddities in PowerShell:

    PowerShellTraps is a collection of some PowerShell traps and oddities shown by demo scripts and automated tests.