title: PowerShell - Setting $ErrorActionPreference for the entire script
body: <p>I've been giving PowerShell (v3.0) a shot for the first time today, and became enormously frustrated with the strange way some of its error-handling concepts are implemented.</p>
  
  <p>I wrote the following piece of code (using the Remote Registry PowerShell Module)</p>
  
  <pre><code>try
  {
      New-RegKey -ComputerName $PCName -Key $Key -Name $Value
      Write-Host -fore Green ($Key + ": created")
  }
  catch
  {
      Write-Host -fore Red "Unable to create RegKey: " $Key
      Write-Host -fore Red $_
  }
  </code></pre>
  
  <p>(This is just a snippet)</p>
  
  <p>Apparently the default behavior of PowerShell is to NOT catch errors which are non-terminating. So I added the following line at the top op my script, as recommended by various people:</p>
  
  <pre><code>$ErrorActionPreference = "Stop"
  </code></pre>
  
  <p>Executing this in the PowerShell ISE did indeed catch all errors. However, execution following command from the terminal still does not catch my errors.</p>
  
  <p><strong>From ISE:</strong></p>
  
  <pre><code>PS C:\windows\system32&gt; C:\Data\Scripts\PowerShell\Error.ps1
  Errorhandling:  Stop
  SOFTWARE\MySoftware does not exist. Attempting to create
  Unable to create RegKey:  SOFTWARE\MySoftware
  Key 'SOFTWARE\MySoftware' doesn't exist.
  </code></pre>
  
  <p><strong>From Command-Line:</strong></p>
  
  <pre><code>PS C:\Data\Scripts\PowerShell&gt; .\Error.ps1
  Errorhandling:  Stop
  SOFTWARE\MySoftware does not exist. Attempting to create
  New-RegKey : Key 'SOFTWARE\MySoftware' doesn't exist.
  At C:\Data\Scripts\PowerShell\Error.ps1:17 char:13
  +             New-RegKey -ComputerName $PCName -Key $Key -Name $Value
  +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,New-RegKey
  
  SOFTWARE\MySoftware: created
  </code></pre>
  
  <p>I have no idea why the behavior of the Preference Variables behaves differently depending on where they are called from, especially since the ISE seems to execute the exact same command?</p>
  
  <p>Based on other feedback, I changed the following line:</p>
  
  <pre><code>New-RegKey -ComputerName $PCName -Key $Key -Name $Value
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>New-RegKey -ComputerName $PCName -Key $Key -Name $Value -ErrorAction Stop
  </code></pre>
  
  <p>Using this method, I was able to trap errors from both the command-line and the ISE, but I don't want to specify the error behaviour on each Cmdlet I call, especially because the catching of errors is essential to the proper functioning of the code. (Plus, the fact that this method DOES work only serves to confuse me even more)</p>
  
  <p>What is the proper way of defining error-handling behavior for the scope of an entire script and/or module?</p>
  
  <p>Also, here's my $PSVersionTable:</p>
  
  <pre><code>PS C:\Data\Scripts\PowerShell&gt; $PSVersionTable
  
  Name                           Value
  ----                           -----
  PSVersion                      3.0
  WSManStackVersion              3.0
  SerializationVersion           1.1.0.1
  CLRVersion                     4.0.30319.18408
  BuildVersion                   6.2.9200.16481
  PSCompatibleVersions           {1.0, 2.0, 3.0}
  PSRemotingProtocolVersion      2.2
  </code></pre>