Win10 Powershell - Simple If/Elseif Depends on Condition Order? Win10 Powershell - Simple If/Elseif Depends on Condition Order? powershell powershell

Win10 Powershell - Simple If/Elseif Depends on Condition Order?


I thought I'd write this out since I can explain it better than my comment. When you enter an expression without assigning it, it gets output to the pipeline. In this case

if ($OS_VERSION = 6) {

is an expression (since the if statement evaluates expressions for a boolean value to take action). If you wrap this in parentheses when entered at an interactive prompt, it'll output what the variable assigns at the same time as assigning the variable, much like

6 | Tee-Object -Variable OS_VERSION

would, or a -PassThru switch on some cmdlets:

> ($Test = 6)>> 6> $Test>> 6

So what you're doing here is always assigning that variable to 6 which evaluates to true since if is truthy and non-zero is true. What you need is the -eq comparison operator:

if ($OS_VERSION -eq 6) {

More information can be found from the following command:

Get-Help -Name about_Comparison_Operators


PowerShell does not use = as a comparison operator.

If you want to compare for equality, the operator is -eq:

if ($OS_VERSION -eq 6) {  Write-Output "OS Version is $OS_VERSION"  # Do stuff...} elseif ($OS_VERSION -eq 10) {  Write-Output "OS Version is $OS_VERSION"  # Do different stuff..}

This will correct your problem. You should take a close look at Get-Help -Name about_Comparison_Operators (or read the link).