title: The term 'else' is not recognized as the name of a cmdlet
body: <p>We have an issue with the If / Else structure in PowerShell. Our scripts that work on former Windows versions (up to Windows Server 2012 R2), do not work on the newer versions.</p>
  
  <p>To simplify things, let's take an example from the PowerShell help (<code>Get-Help about_If</code>):</p>
  
  <pre><code>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."
  }
  </code></pre>
  
  <p>On Windows Server 2012 R2 (PowerShell version based on <code>$PSVersionTable.PSVersion</code> is 5.1.14409.1018) the result is, as expected:</p>
  
  <pre><code>The value  is less than or equal to 2, is not created or is not initialized.
  </code></pre>
  
  <p>However, newer PowerShell Versions, like 5.1.14393.2879 (Windows Server 2016) or 5.1.17763.503 (Windows Server 2019) seem not to understand the same syntax:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>Note, that event these newer PowerShell versions have the very same example for If / Then, so officially nothing should have been changed.</p>
  
  <p>Yes, I know I could easily re-write the condition as:</p>
  
  <pre><code>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."
  }
  </code></pre>
  
  <p>But we have a lot of scripts, and I really would like to avoid to revisit, and fix and test all of them, if there is a way to have PowerShell to understand the other format either. Is this behavior documented officially in the PowerShell language specification, or should we consider it to be a bug?</p>
  
  <p>The example we currently have in the <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_if" rel="noreferrer">web-based documentation</a>, has another format, but it does not work either (same error) with the newer versions:</p>
  
  <pre><code>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.")
  }
  </code></pre>
  
  <p>I've found a <a href="https://unlockpowershell.wordpress.com/2012/08/21/powershell-version-3-if-else-bug" rel="noreferrer">page</a> discussing a similar issue, but it does not match exactly our case.</p>
  
  <p>Very annoying. Thanks for your help in advance.</p>
  
  <p><strong>Update 1</strong>: If I save the script into a .ps1 file, and execute the file, then it seems to work. I have only the problem, if I copy / paste the code block from the editor (Notepad in my case).</p>
  
  <p><strong>Update 2</strong>: 
  <code>$PSVersionTable</code> results:</p>
  
  <pre><code>Name                           Value
  ----                           -----
  PSVersion                      5.1.17763.503
  PSEdition                      Desktop
  PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
  BuildVersion                   10.0.17763.503
  CLRVersion                     4.0.30319.42000
  WSManStackVersion              3.0
  PSRemotingProtocolVersion      2.3
  SerializationVersion           1.1.0.1
  </code></pre>
  
  <p>By copy-pasting the condition from Notepad into the PowerShell console I have this error in the newer versions (I had no error with  Windows Server 2012 R2):</p>
  
  <pre><code>if ($a -gt 2)
  &gt;&gt; {
  &gt;&gt;     Write-Host "The value $a is greater than 2."
  &gt;&gt; }
  PS C:\Scripts&gt; else
  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.
  At line:1 char:1
  + else
  + ~~~~
      + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
  </code></pre>
  
  <p><strong>Update 3</strong>: 
  As always, the devil is in the details. I've pasted the content into the console by using the right mouse button, since in WS2012R2 (and older) the standard key combination for pasting <code>Ctrl+V</code> did not work, and resulted in a <code>^V</code>. So I got used to pasting the script into the console by clicking the right mouse button (and on WS2012R2 selecting Paste from the context menu). Apparently, the paste funtionality has been implemented differently, and the content is interpeted line-by-line for the right-click, but interpreted as a block when pasted using <code>Ctrl+V</code>. Nice. Have learned something new again. Thank you all for your support, and for the ideas!</p>