More $profile madness

April 13th, 2013 by

As usual, a minor glitch in my workflow has me mucking around with my $profile. It all started because I normally run Word, Excel, and other Office applications from the command line (as I do many non-MS applications.) To do this, I have functions and aliases in my $profile. These let me start Word, for example, by typing “word” on the PowerShell command line along with any filenames I want to open. So far, so good. But I recently started using Office 2013 on some machines, and this broke my functions for those machines. And at least one of them is running the 640-bit version of office. Hmmm. That could just be a problem. Now I could manually edit the functions in those $profiles, but that’s just kludgy. I like to have a single $profile that I load on any new machine I work on. So, rather than the hard-coded paths, which were never really elegant, but worked fine when I only had one version running, it’s time to be a bit more general. So, I added the following function to my $profile:

Function Find-Office {
   # if on 32-bit, or running 64-bit office on 64-bit machine, this will find office
   $Global:[[code]]czo2Olwib2ZmaWNlXCI7e1smKiZdfQ==[[/code]] = Get-ChildItem `
              -file "Outlook.exe" `
              -path "C:\Program Files\Microsoft Office\" `
    if (! $office) {
    # If we didn't find it there, look down the (x86) path. But only if we didn't find it
    $Global:[[code]]czo2Olwib2ZmaWNlXCI7e1smKiZdfQ==[[/code]] = Get-ChildItem `
              -file "Outlook.exe" `
              -path "C:\Program Files (x86)\Microsoft Office\" `

Now, a couple of comments here — I made the $Office variable Global so that I could get at the value outside of my profile, since there’s a fair amount of information and properties in it.  The other thing is that while I’m searching for Outlook, it really could have been any of the core Office apps that I know get installed every time I install Office. If you’re running Office Home & Student edition, however, you’d best choose a different executable, such as “winword.exe”.

So, now that we know where Office is, we can extract the path to it:

$OffPath = (Find-Office).DirectoryName

(note, we’re still inside my $profile, but we won’t bother to make $OffPath global, since we can easily get it again if we need it. )

Finally, the functions that our aliases will call to start the core Office applications:

function Run-Word { & "$OffPath\WinWord.exe" $args}
function Run-Excel { & "$OffPath\Excel.exe" $args }
function Run-OneNote { & "$OffPath\OneNote.exe" $args }
function Run-Outlook { & "$OffPath\Outlook.exe" $args }

The aliases are simply calls to these functions in the form:

set-alias word Run-Word

Nothing fancy there. But now I’ve got easy command line access to my core Office applications. (As you can see, I don’t use PowerPoint any more than I have to. :))

Posted in PowerShell | Comments Off on More $profile madness

Comments are closed.