Hi @Jeff B
You can use the following PowerShell command to hide:
CreateObject("Wscript.Shell").Run "foo.bat", 0, True
--------------------------------------
If this is helpful please accept answer.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
Hi @Jeff B
You can use the following PowerShell command to hide:
CreateObject("Wscript.Shell").Run "foo.bat", 0, True
--------------------------------------
If this is helpful please accept answer.
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
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
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