Powershell Script - Hide cmd Window?

Jeff B 1 Reputation point
2022-08-27T02:21:15.24+00:00

I have a simple PS script that alternates/displays open Chrome windows (not tabs) after a preset time delay. The script is run with a .bat file on my desktop. It works fine but the cmd prompt window is one of the windows that cycles thru (even with the .AppActivate (Chrome) command. (This also happens if I run the script using PS ISE. The ISE window is one of the windows that displays during execution.)

I don't want to close or "stop" the cmd window and therefore stop the script, just hide it as one of the open windows. Is there a way to do this either in the script or the .bat file?

Thanks.

--Jeff

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,446 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Dillon Silzer 55,086 Reputation points
    2022-08-27T02:40:01.36+00:00

    Hi @Jeff B

    You can use the following PowerShell command to hide:

    CreateObject("Wscript.Shell").Run "foo.bat", 0, True

    Cited from https://stackoverflow.com/questions/33685398/how-not-to-open-a-cmd-window-when-running-a-batch-file#:~:text=You%20cannot%20hide%20the%20cmd,which%20hides%20the%20cmd%20window.&text=You%20could%20put%20powershell%20%2Dwindow,command%20%22%22%20in%20your%20script.

    --------------------------------------

    If this is helpful please accept answer.

    0 comments No comments

  2. Jeff B 1 Reputation point
    2022-08-27T04:29:44.957+00:00

    Perfect!!!!

    I'm a newbie with Powershell, .bat commands, and .vbs files. But I read the thread in the link and got that it needed to be a separate .vbs file with this single line...calling my .bat file. I created it in notepad, added the .bat filename (with path), and saved it as .vbs file also on my desktop.

    Thanks for the solution!!!!

    --Jeff

    0 comments No comments

  3. Jeff B 1 Reputation point
    2022-08-27T05:14:16.947+00:00

    Ok...now that it works perfectly, how to I stop the script without shutting down Chrome altogether? I'd like to do so with a keystroke. Cntrl+C didn't stop the script. I tried a [System.Console]::KeyAvailable line in the PS script but I got an error that it can't detect a keystroke without an open console. And the Console.in.Peek command didn't quite work either. I'm clearly a newbie just trying things semi-randomly (learn by doing?). But any additional help appreciated.

    --Jeff

    0 comments No comments

  4. MotoX80 32,556 Reputation points
    2022-08-27T13:04:58.733+00:00

    I would suggest doing it like this using a flag file.

    Create a Start.bat file.

    del C:\temp\scripts\stop.flg  
    start powershell.exe -WindowStyle Hidden -File C:\temp\Scripts\MyScript.ps1   
    

    Create a Stop.bat file.

    echo stop > C:\temp\scripts\stop.flg  
    

    Your script file would look like this. Since it won't have a window to view the output, you should create a log file where you can document key processing events.

    $log = "C:\Temp\Scripts\MyScript.log"  
    $StopFlag = "C:\temp\scripts\stop.flg"  
    "{0} - MyScript starting." -f (Get-Date) | Out-File -FilePath $log -Append  
    while ($true) {  
        "{0} - Main loop executing.." -f (Get-Date) | Out-File -FilePath $log -Append  
        # application code goes here.  
        Start-Sleep -Seconds 5             # Sleep for some period   
        if (Test-Path $StopFlag) {  
           "{0} - Stop flag detected." -f (Get-Date) | Out-File -FilePath $log -Append  
           remove-item $StopFlag  
           break  
        }  
    }  
    "{0} - MyScript ending." -f (Get-Date) | Out-File -FilePath $log -Append  
      
    
     
    
    0 comments No comments