Weekend Scripter: Control Processes with PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to control processes.
Microsoft Scripting Guy, Ed Wilson, is here. This morning I am made a pot of Masala Chai. I found a pretty good recipe when I was doing a Bing search: Indian Masala Chai. One of my pet peeves is when people refer to it as Chai tea, which is rather silly because the word chai simply means tea, so they are literally saying “tea tea.” Anyway, I spent a decent amount of time heating milk, finding spices, and making a nice pot of black Assam tea for the starter. It was a nice relaxing way to start a morning, and my efforts are rewarding.
That is the way it is with lots of things, a little bit of effort soon becomes very rewarding. This is especially true with Windows PowerShell. I probably use Windows PowerShell every day—on my laptop, on my Surface, on my servers.
Whenever I find myself doing the same thing over and over and over again, that is a good opportunity for me to investigate using the power of Windows PowerShell.
One thing I do every day is start the day by turning on my computer, logging in, and then launching the same three applications: Outlook, Lync, and Word. It is always the same three applications: Outlook, Lync, and Word. I may also open Internet Explorer, but that is always after I have launched Outlook, Lync, and Word.
And you know what? Those are also probably the three slowest applications to launch. This means that after I log in, I click and wait, wait, wait, wait, wait. I click again and wait and wait and wait, and click once more.
Finally I decided, that is stupid. You know why? Because I also always launch Windows PowerShell. So I wrote a very simple script that launches my three applications:
sleep -Seconds 3
sleep -Seconds 2
Start-Process Winword.exe -WindowStyle Minimized
I usually look at…no, I take that back, I always look at my email first. So I do not need to have Word (the process name is Winword) maximized. So I use the WindowStyle parameter to launch Word as minimized. I tried that with Lync, but for some reason it did not work, and Lync stayed maximized anyway. Oh well.
Another thing that did not work was using the –Wait parameter. The Start-Process cmdlet has a –Wait parameter that I wanted to use. I wanted it to suspend execution of the script until the application was fully launched. The problem is that Outlook and Lync never seem to complete launching—that is, they are always doing something in the background…syncing address lists, updating status, checking for new email and so forth and so on. So the –Wait parameter would simply hang the script.
To prevent that, I decided to give Outlook three seconds and Lync two seconds before launching the next process. In reality, on my poor old laptop, this is not nearly enough time. It seems that Outlook wants at least 10 seconds to become halfway usable, but there was no way I was going to wait around that long for the script to complete.
So what do I do with my script? Well, I put it in my easy-to-access folder, and I use the Task Scheduler to launch the script at logon, but I delay it for 15 minutes. This permits me to log on to my laptop, and then go make a pot of tea. This gives my laptop time to settle down, and my applications are waiting for me when I come back. Perfect solution! The script was easy to write, and I used the GUI to schedule my script because it is a one-off sort of thing.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy