title: How Can I handle Errors in PS
body: <p>I'm trying to catch the error from service restart using function. The issue Im having that catch is not doing anything through the script.</p>
  <pre><code>Function Service_Restart ($Servers, $Service){
      try {
      Write-Host &quot;$CurrentDateTime - Restarting Service $Service on Servers $Servers&quot;
      ForEach($Serv in $Servers){
          
  
              Get-Service -ComputerName $Serv -Name $Service | Restart-Service
              
          }
          
      
      } catch [System.ServiceProcess.ServiceController] { 
          
          $error[0].Exception
  
          
  }
  
  Service_Restart Server1 ServiceName
  
  </code></pre>
  <p>I'm expecting to see an one line error not the full stack, however I see the entire error.</p>
  <p>What I like to see as an example</p>
  <pre><code>Restart-Service : Service 'Adobe Acrobat Update Service (AdobeARMservice)' cannot be stopped due to the following error: Cannot open AdobeARMservice service on computer 'Localhost'.
  </code></pre>
  <p>However I'm seeing</p>
  <pre><code>Restart-Service : Service 'Adobe Acrobat Update Service (AdobeARMservice)' cannot be stopped due to the following error: Cannot open AdobeARMservice service on computer 'Localhost'.
  At C:\user\UTILS_Version3.ps1:48 char:62
  + ...      Get-Service -ComputerName $Serv -Name $Service | Restart-Service
  +                                                           ~~~~~~~~~~~~~~~
      + CategoryInfo          : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
      + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.RestartServiceCommand
  
  </code></pre>
  <p>If I run  $error[0].Exception manually I get the result I'm looking for but not through the function.</p>
  <pre><code>PS C:\Users\&gt;  $error[0].Exception
  
  Service 'Adobe Acrobat Update Service (AdobeARMservice)' cannot be stopped due to the following error: Cannot open AdobeARMservice service on computer '127.0.0.1'.
  PS C:\Users\&gt; 
  </code></pre>