title: Excel VBA - Powershell don't exit | How to hide CMD
body: <p>I have this code that I am running: </p>
  
  <pre><code>Public Function checkhost(hostname As String)
      'bla bal...
      strexec = "powershell -WindowStyle Hidden -Command ping " &amp; hostname &amp; " -n 1"
      Set oExec = WshShell.exec(strexec)
      'bla bla...
  End Function
  </code></pre>
  
  <p>From my Excel document, the shell does not close - it remains open. The same code does work, if I use it in Powershell.</p>
  
  <p>With cmd it works fine but I want to hide the cmd window.</p>
  
  <pre><code>strexec = "%comspec% /C %WINDIR%\system32\ping.exe " &amp; hostname &amp; " -n 1"
  Set oExec = WshShell.exec(strexec)
  </code></pre>
  
  <p>AFAIK the only way to hide CMD is to use WshShell.Run but I don't know how to change my existing code. Can anyone suggest a solution?</p>
  
  <p><strong>Complete code</strong></p>
  
  <pre><code>    Public Function checkhost(hostname As String)
  
      Dim WshShell As Variant
      Set WshShell = CreateObject("Wscript.Shell")
  
      Dim ReturnErrorLevel As Variant
      strexec = "powershell -WindowStyle Hidden -Command ping " &amp; hostname &amp; " -n 1"
      Set oExec = WshShell.exec(strexec)
  
  
  
      Do Until oExec.Status = 1
          Application.Wait (Now + TimeValue("00:00:01"))
      Loop
  
      Result = Split(oExec.StdOut.readAll, vbCrLf, , vbTextCompare)
  
      For Each Item In Result
          Debug.Print Item
          If InStr(1, Item, "Ping request could not find host", vbTextCompare) &gt; 0 Then
              checkhoststatus = "0.0.0.0"
              Exit For
          End If
  
          If InStr(1, Item, "Request timed out.", vbTextCompare) &gt; 0 Then
              checkhoststatus = "0.0.0.0"
             Exit For
          End If
  
          If InStr(1, Item, "Reply from ", vbTextCompare) &gt; 0 Then
              checkhoststatus = Mid(Item, 12, InStr(1, Item, ":", vbTextCompare) - 12)
              Exit For
          End If
  
      Next Item
  End Function
  </code></pre>