Ramblings of a software developer

PowerShell Console “Select Mode”?!

It’s 2013 and one of my resolutions was to actually get off my ass and start blogging here and there about things I have learned, problems I’ve solved, or just whatever I feel like. So far I have been failing at this miserably, given that 2013 started over a month ago but I’m going to defer to the cliche “better late than never”.

So today I was doing some moving around of a bunch of files and whatnot on a remote computer and I thought to myself, “this is painful, I should script this”. So I did. I wrote a PowerShell script to loop through a bunch of folders, get their contents and move them to another directory for processing. Simple right? It should have been, but I suck at PowerShell so I have to look up every single command ands it’s parameters all the time. I just can’t seem to remember the crazy names they have for everything. That aside, after a few tries I managed to get what I wanted and all was well and good, on my development machine.

When I went to run my script on the remote computer, I found that it would randomly start hanging and not proceed to move to subsequent directories. I would check every once and a while to see the progress of the script (I printed out completed folders to the console), and often found that it would have stopped processing after the first couple items and I had wasted an hour or two doing nothing. Frustrating.

I tried changing the way my script worked, and at one point I thought I had fixed it, but then I checked a short while later and it stopped again. I was almost ready to give up when one of my coworkers stopped by and inquired as to what my problem was. I told him and he responded with “Oh you’re in select mode, the script pauses when you are in select mode”. Select mode? What the hell is select mode and how can you tell? “Oh the title bar says Select, just right click to get out of it”.

At this point, I was both relieved and furious. I had wasted a significant amount of time trying to debug my script when it was the stupid console window that was the problem. When I would click on the console window to focus it on the screen I would often accidentally select some empty space on the window which would put the console window into “Select Mode” which pauses script execution. Nothing was wrong with my script.

I have never run into this problem before because I hate the default windows powershell console, I use Console2, however, on the remote machine I had to use the default console application that comes with PowerShell.

No console application I have ever used before has this feature (that I am aware of) so it wasn’t even something that was on my radar for troubleshooting. To start with, it is very non-obvious that you are even in “Select Mode” when using the PowerShell command prompt (just a simple “Select” added to the title bar). Not only this, but you have to already know what “Select Mode” means, i.e., that it suspends the execution of the script. I feel like the console could indicate that it was paused in a more obvious manner, perhaps by saying “Execution Paused” or something equally informative, instead of “Selected”. I also think a change in color of the window or the title bar would be appropriate to indicate a state change.

Suffice to say, if it appears your PowerShell script is hanging while trying to run it using the standard PowerShell console, make sure you aren’t in “Select Mode”. If you are, right click your problems away.