Start daily programs – More n00bz fun with #Powershell!

When I get in to work in the morning, I like a good cup of coffee waiting for me, a neck rub, and all my favorite programs open and waiting for me.  Since the first two seem out of the question, I focus on the third.

I’ve automated my start-of-day set of programs before by other means, but it’s awfully fun to see how PowerShell does it. So I did.

First, the PowerShell itself:

## Open Outlook ##
# From script on http://stackoverflow.com/questions/3675533/powershell-to-open-outlook-make-visible #
$outlook = new-object -com Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$folder = $namespace.GetDefaultFolder("olFolderInbox")
$explorer = $folder.GetExplorer()
$explorer.Display()

## Open IE with several tabs ##
#Modified from script on http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/9d003109-0903-414b-a635-ca2a9c485381/  #
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate2("about:Tabs",0x1000)
$ie.Navigate2(https://myemail.com/)
$ie.Navigate2("www.twitter.com",0x1000)
$ie.Visible = $true

## Open SSMS ##
# Inspired by http://technet.microsoft.com/en-us/library/ee176882.aspx  #
Invoke-Item c:\temp\blank.sql

## Open a Word document ##
#Modified from script on http://powershellscripts.blogspot.com/2007/02/convert-or-export-word-documents-to-pdf.html  #
$word = new-object -ComObject "word.application"
$doc = $word.documents.open("c:\path\to\My Documents\Status.doc")

#Note: This script will only finish after SSMS receives the required authentication #

A couple of note: Clearly, I got all these scripts from digging around on Google, and some playing with Powershell Help. I like that I can open multiple tabs in IE; I leave one tab blank for Pandora, which would start playing automatically if I started it with this…and I don’t want music playing from my computer without me here.  I didn’t find an easy way to just open SSMS – I’m sure somebody will provide me the answer in Comments below – so I just opened a blank SQL file to do the trick.

Next, I created a BAT file with the PowerShell command powershell -command “& ‘c:\command\Open_My_Programs.ps1’ “  I saved this BAT file and the POSH file in c:\command\

Finally, I scheduled this to run right before I start my work day, using Windows Task Scheduler (see the Control Panel).  The scheduled task uses a command prompt, and I used advanced options to change the command to C:\WINDOWS\system32\cmd.exe /C “c:\command\Open_My_Programs.bat” and to kill the process if it runs for 90 minutes. (If I”m not at the office to log into SSMS, no point it letting the POSH script hang).

I haven’t tested this yet (how’s that for lazy blogging?), but as I understand it you kinda have to put the Powershell call in a batch file, because of the double quotes.

So there you go, a quick, dirty, and fun way to feel like the rock star IT guy you know you are.

By-the-by, I’m reading through Learn Windows PowerShell in a Month of Lunches by Don Jones (link goes to my review on ITBookworm.com), and I HIGHLY recommend it!

Happy days,
Jen McCown
http://www.MidnightDBA.com/Jen

5 thoughts on “Start daily programs – More n00bz fun with #Powershell!

  1. Michael Essen

    I use Windows Scheduler quite a bit with PS. I usually just start the script that i’m running by calling Powershell.exe with the argument of the full path of the Script that I want to run, and the optional parameter “–Noninteractive”.

    Keeps me from having to store an additional bat file for each script that I’m wanting to schedule.

    I’m sure there is a downside to doing it this way, just haven’t run across it yet.

  2. Sean McCown

    Yeah, you don’t need a .bat file to run this. Just put it in a .ps1 and call that from your job.

    The SSMS thing is easy too, and more flexible than you’ve got it. SSMS puts itself in your path so you can call it from anywhere. So starting it up in PS is as easy as just:
    >SSMS

    That’ll start it. Now, you can pass it params as well to connect to certain servers, etc. If you’re at the cmd just type:
    >ssms /?
    and you’ll see a list of options you can use when starting it up.

  3. Ryan Adams

    I do this as well, but mine are still written in VBScript. I know…I know…slap on the hand. I even use Word to put the focus on a Java App because Java hides it when you open it, but Word can enumerate the open app. I also use a keystrokes method since I can’t access it programmatically so it enters it as if I were typing it.

  4. Pingback: Something for the Weekend – SQL Server Links 30/09/11

Comments are closed.