{"id":39,"date":"2007-01-08T07:00:00","date_gmt":"2007-01-08T15:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/08\/taking-shortcuts-in-powershell\/"},"modified":"2007-01-08T07:00:00","modified_gmt":"2007-01-08T15:00:00","slug":"taking-shortcuts-in-powershell","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/08\/taking-shortcuts-in-powershell\/","title":{"rendered":"Taking Shortcuts in PowerShell"},"content":{"rendered":"<p>Last month I wrote a blog <a href=\"http:\/\/sapien.eponym.com\/blog\/_archives\/2006\/12\/18\/2581354.html\">entry <\/a>about re-discovering DOSKey. I have to say it has been very useful and I feel much more efficient.&nbsp; 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.&nbsp; However, there are times when I&#8217;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.<\/p>\n<p>Your default user PowerShell profile is in %My Documents%\\WindowsPowerShell\\Profile.ps1.&nbsp; Let me show you what I did.<\/p>\n<p>In my DOSKey macro file I have a macro defined like this:<\/p>\n<p>op=&#8221;%programfiles%\\Opera\\opera.exe&#8221; <\/p>\n<p>In my PowerShell profile I created a new alias with the same name:<\/p>\n<p>Set-Alias op&nbsp; $env:programfiles\\Opera\\opera.exe<\/p>\n<p>Notice I substituted $env:programfiles which returns the same path as %programfiles%.&nbsp; With this alias I can launch Opera with the same keyboard shortcut as I do in CMD.&nbsp; One thing to be careful of is spaces.&nbsp; For example, my DOSKey macro of<\/p>\n<p>wd=&#8221;%programfiles%\\Microsoft Office\\Office11\\winword.exe&#8221;<\/p>\n<p>becomes<\/p>\n<p>Set-Alias wd&nbsp; $env:programfiles&#8221;\\Microsoft Office\\Office11\\winword.exe&#8221;<\/p>\n<p>Notice I have to include quotes in the path.<\/p>\n<p>Some of my DOSKey macros are CMD expressions. Consider this macro:<\/p>\n<p>cm=%windir%\\system32\\compmgmt.msc \/computer=$1<\/p>\n<p>In PowerShell, I created a function to duplicate this functionality:<\/p>\n<p>function Get-ComputerManagement {<br \/>Param([string]$computer) mmc $env:windir\\system32\\compmgmt.msc \/computer=$computer<br \/>}<\/p>\n<p>Once this function is defined, I can define an alias for it:<\/p>\n<p>Set-Alias cm Get-ComputerManagement<\/p>\n<p>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.&nbsp; Anyway, now I have shortcuts at my fingertips regardless of what shell I&#8217;m using.<\/p>\n<p>Here are a few more aliases you can try out:<br \/>Set-Alias pse $env:programfiles&#8221;\\sapien\\primalscript enterprise\\primalscript.exe&#8221;<br \/>Set-Alias np&nbsp; $env:WinDir\\notepad.exe<br \/>Set-Alias tb $env:programfiles&#8221;\\mozilla thunderbird\\thunderbird.exe&#8221;<br \/>Set-Alias wd&nbsp; $env:programfiles&#8221;\\Microsoft Office\\Office11\\winword.exe&#8221;<br \/>Set-Alias xl&nbsp; $env:programfiles&#8221;\\Microsoft Office\\Office11\\Excel.exe&#8221;<br \/>Set-Alias paint $env:Windir\\System32\\mspaint.exe<br \/>Set-Alias ie&nbsp; $env:programfiles&#8221;\\Internet Explorer\\iexplore.exe&#8221;<br \/>Set-Alias fp&nbsp; $env:programfiles&#8221;\\Microsoft Office\\Office11\\Frontpg.exe&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,25],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}