title: How can I tell if Docker Desktop is running in a Powershell script?
body: <p>I have a Powershell script in which I need to know if Docker Desktop is currently running.</p>
  <p>I have read that one way to do it is to use <code>docker ps</code> and see if I get back a container list or an error message. But I'm struggling to make use of this in an actual script. I haven't worked out how to capture and act on the result correctly.</p>
  <p>Here's what I have so far:</p>
  <pre class="lang-powershell prettyprint-override"><code>function IsDockerDesktopRunning()
  {
      $dockerPsResult = docker ps | Out-String
  
      Write-Host &quot;Result is:&quot;
      Write-Host $dockerPsResult
  
      if (($dockerPsResult).StartsWith(&quot;error&quot;))
      {
          return $false
      }
      return $true
  }
  
  $isRunning = IsDockerDesktopRunning
  Write-Host $isRunning
  </code></pre>
  <p>When when it's <em>not</em> running, the result looks like this:</p>
  <pre><code>error during connect: This error may indicate that the docker daemon is not running.: Get &quot;http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/json&quot;: open //./pipe/docker_engine: The system cannot find the file specified.
  Result is:
  
  True
  </code></pre>
  <p>As you can see, it looks the output from the <code>docker ps</code> command gets output as it's executed and the variable isn't populated.</p>
  <p>Is there a way to correctly capture the output?
  <strong>Or</strong>, should I tackle the problem a different way?</p>