Consider the following from PowerShell’s excellent help:
Get-Help about_If
001
002 003 004 005 006 007 008 |
if ($a -gt 2)
{ Write-Host “The value $a is greater than 2.” } else { Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.” } |
If you modify that example to tighten it up a bit, and make it look better you can cause the else to fail.
Consider the following:
001
002 003 004 005 006 |
if ($a -gt 2) {
Write-Host “The value $a is greater than 2.” } else { Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.” } |
The above example still works, and uses two less lines of code.
But, suppose we wanted to make it even shorter, Do we really need the closing curly braces on separate lines? No, we don’t. This works as well, but is not as easy to follow.
001
002 003 004 |
if ($a -gt 2) {
Write-Host “The value $a is greater than 2.”} else { Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.”} |
Suppose we didn’t want the “Write-Host statements on a separate line – after all, they are only one line each. The following will fail:
001
002 |
if ($a -gt 2) {Write-Host “The value $a is greater than 2.”}
else {Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.”} |
else : The term ‘else’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
The following will work however:
001
002 003 |
if ($a -gt 2) {
Write-Host “The value $a is greater than 2.”} else {Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.”} |
As do all of the following:
001
002 003 |
if
($a -gt 2){Write-Host “The value $a is greater than 2.”} else{Write-Host “The value $a is less than 2 or was not created or initialized.”} |
001
002 003 |
if ($a -gt 2) {Write-Host “The value $a is greater than 2.”
} else {Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.”} |
001
002 |
if ($a -gt 2) {Write-Host “The value $a is greater than 2.”
}else {Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.”} |
001
002 003 |
if ($a -gt 2)
{Write-Host “The value $a is greater than 2.”} else {Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.”} |
001
002 |
if ($a -gt 2)
{Write-Host “The value $a is greater than 2.”}else {Write-Host “The value $a is less than or equal to 2, is not created or is not initialized.”} |
The examples above get real ugly real fast, but they work.
To further muddy the water, the following will also work:
001
002 003 004 |
if ($a -gt 2){
Write-Host “The value $a is greater than 2.”} elseif ($a -eq 2){Write-Host “The value $a is equal to 2.”} else{Write-Host “The value $a is less than 2 or was not created or initialized.”} |
It only fails if the closing curly brace is on the same line as the if keyword.
I’ve filed a bug on connect – if you think it’s worthwhile, vote it up. You cqn do that by signing in, and clicking the + sign in the green triangle:
#1 by cavallogolooso on September 5, 2012 - 16:23
Reblogged this on Depresso Gioioso and commented:
Mapporc…!!!! Comunque non ho capito se posso installare la versione italiana
#2 by koenraadrens on February 21, 2013 - 02:59
I can’t reproduce this. Maybe fixed in RTM? I’m using Windows 7 SP1 with WMF 3.0.
#3 by Karl Mitschke on April 16, 2013 - 11:44
I still have this behavior with PowerShell Version 3. on Windows 8….
Karl
#4 by Sebastian on November 3, 2014 - 06:28
I have this bug in v4 as well on windows 8.1
#5 by Karl Mitschke on January 5, 2015 - 15:52
Yes, I still have the bug as well.
Karl