Last month I wrote a blog entry about re-discovering DOSKey. I have to say it has been very useful and I feel much more efficient. With only a few keystrokes I can kick off programs or complex command expressions. This was easy to do since I always have a CMD window open. However, there are times when I’m in PowerShell and find myself reaching for a keyboard shortcut. While it is possible to access CMD from within Powershell, it takes too many steps. What I want is to use the same shortcut in PowerShell that I do in CMD. The solution is customizing my PowerShell profile with functions and aliases.
Your default user PowerShell profile is in %My Documents%\WindowsPowerShell\Profile.ps1. Let me show you what I did.
In my DOSKey macro file I have a macro defined like this:
op=”%programfiles%\Opera\opera.exe”
In my PowerShell profile I created a new alias with the same name:
Set-Alias op $env:programfiles\Opera\opera.exe
Notice I substituted $env:programfiles which returns the same path as %programfiles%. With this alias I can launch Opera with the same keyboard shortcut as I do in CMD. One thing to be careful of is spaces. For example, my DOSKey macro of
wd=”%programfiles%\Microsoft Office\Office11\winword.exe”
becomes
Set-Alias wd $env:programfiles”\Microsoft Office\Office11\winword.exe”
Notice I have to include quotes in the path.
Some of my DOSKey macros are CMD expressions. Consider this macro:
cm=%windir%\system32\compmgmt.msc /computer=$1
In PowerShell, I created a function to duplicate this functionality:
function Get-ComputerManagement {
Param([string]$computer) mmc $env:windir\system32\compmgmt.msc /computer=$computer
}
Once this function is defined, I can define an alias for it:
Set-Alias cm Get-ComputerManagement
The only thing you have to remember is to define all your functions first in your profile then use Set-Alias to create your keyboard shortcuts. With a little work I was able to duplicate my DOSKey macros in PowerShell. In fact, I realized there were many PowerShell functions I use, such as a free space function, that I created even more aliases. Anyway, now I have shortcuts at my fingertips regardless of what shell I’m using.
Here are a few more aliases you can try out:
Set-Alias pse $env:programfiles”\sapien\primalscript enterprise\primalscript.exe”
Set-Alias np $env:WinDir\notepad.exe
Set-Alias tb $env:programfiles”\mozilla thunderbird\thunderbird.exe”
Set-Alias wd $env:programfiles”\Microsoft Office\Office11\winword.exe”
Set-Alias xl $env:programfiles”\Microsoft Office\Office11\Excel.exe”
Set-Alias paint $env:Windir\System32\mspaint.exe
Set-Alias ie $env:programfiles”\Internet Explorer\iexplore.exe”
Set-Alias fp $env:programfiles”\Microsoft Office\Office11\Frontpg.exe”