title: PS script to check status of a service and install a new windows service
body: <p>PS Script Scenario</p>
  
  <pre><code>1-&gt; Check if service exists
       -&gt; if doesn't exists
          -&gt; copy item and install windows service and start.
       -&gt; If exists
          -&gt; Stop Windows service &amp; delete it and copy item from folder and Install &amp; start the service.
  </code></pre>
  
  <p>Below is my script</p>
  
  <pre><code>$service = Get-Service -Name XXX -Computername YYY
  if($service.Status -eq $NULL)
  {
      Copy-Item "C:\location\*" "\\yyy\d$\Location" -Force -Recurse
      sc.exe \\yyy create xxx start=auto DisplayName="value"  binPath= D:\Build\test.exe
      sc.exe \\yyy description xxx "value"
      sc.exe \\yyy start xxx
      Write-Host "xxx STARTED"
  }
  else
  {
      sc.exe \\yyy stop xxx
      Write-Host "xxx STOPPED"
      sc.exe \\yyy delete xxx 
      Write-Host "xxx DELETED"
      Copy-Item "C:\Location\*" "\\yyy\d$\Location" -Force -Recurse
      sc.exe \\yyy create xxx start=auto DisplayName="value"  binPath= D:\Build\test.exe
      sc.exe \\yyy description xxx "value"
      sc.exe \\yyy start xxx
      Write-Host "xxx STARTED"
  }
  </code></pre>
  
  <p>Is my approach right and I see error if there is no service name XXX. How to override this error and continue with the If condition statement. </p>
  
  <p>Error-</p>
  
  <pre><code>Get-Service : Cannot find any service with service name 'Spoole'.
  At line:1 char:12
  + $service = Get-Service -Name Spoole
  +            ~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ObjectNotFound: (Spoole:String) [Get-Service], ServiceCommandException
      + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
  </code></pre>