Handy Hack: Minimize A Running Batch File Window

Every some often we all come across a problem that there's no real good way to solve. In which case we have to come up with a workaround. They're not always pretty. Sometimes we're surprised they even work at all. We don't even try to gussy them up because putting lipstick on a Frankenstein doesn't make them any less scary. We just call them what they are.
They're hacks.
One reason I started this blog was to post useful things that I came across whether it be hacks, an algorithm, or just some advice. I want this to be the place that has the information I couldn't easily find or has a more in depth explanation on how something works. And with saying that, I'm going to start posting more short entries starting with this one.
Like most random useful tricks and hacks, I figured this out while investigating a completely different problem. I've never found an automated way to minimize, or maximize, a running DOS batch file window. I know I've seen other people searching for methods of doing this as well because I've seen their posts on various forums while searching myself. So to my surprise I ran across this while debugging a PowerShell issue my team was having.
Yes. This does require PowerShell to be installed on your machine. This is a hack. They're not always elegant and they often rely on the behavior of something outside of what you would've expected or wanted to rely on. But to the best of my knowledge PowerShell is installed as part of Vista and up. So it's not reaching too out of the norm to pull this off.
If you look at the options for PowerShell you'll see that there's a -WindowStyle flag. The choices for it are Normal, Minimized, Maximized, and Hidden. Using this flag when you start PowerShell will place the window in the selected state.
“Ok,” you may be saying to yourself, “That's great, but now my batch file is blocked until I quit PowerShell.” And this is true. But PowerShell also has the -Command flag which allows you to run a command and then exit when finished. Why not use this flag to execute the “exit” command so PowerShell quits immediately?
Folks, we have ourselves a hack. Now shake your head in disbelief that someone suggested it, give a sigh of defeat for lack of a better solution, and then copy and paste it into your script.
Below are examples of how to do this which can be ran from a DOS prompt. I'm also using -NoLogo just because it makes things cleaner and -NoProfile to speed up the loading of PowerShell. The ordering doesn't matter exception for -Command which must be the last flag. There's also the -NonInteractive flag. I haven't played with this too much and since PowerShell is only running briefly, it's probably not needed, but doesn't hurt either.
To minimize the window:
C:\>PowerShell.exe -NoLogo -NoProfile -WindowStyle Minimize -Command exit
To maximize the window:
C:\>PowerShell.exe -NoLogo -NoProfile -WindowStyle Maximize -Command exit
To return the window to normal (its default size):
C:\>PowerShell.exe -NoLogo -NoProfile -WindowStyle Normal -Command exit
This next one you have be careful with. It will hide your window, remove it from the Applications tab in Task Manager (it does still shows up under the Processes tab), and at least on Windows 7, no longer show up on the task bar. I haven't played with this enough to figure how to make it visible again. You have been warned.
To hide the window:
C:\>PowerShell.exe -NoLogo -NoProfile -WindowStyle Hidden -Command exit
Now these are shown using the command line, but you can just drop them into a batch file to get the same effect. I added the minimize command to an existing script that's launched by Task Scheduler as me. This way I no longer have a black box jumping out of nowhere, breaking my concentration, causing alarm while I determine what's happening, and then realize it's just copying files so I minimize it.
It's the little things that make life grand.
Happy scripting!



