{"id":1106,"date":"2009-01-29T00:00:30","date_gmt":"2009-01-29T08:00:30","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/01\/29\/sneaky-aliases\/"},"modified":"2009-01-29T04:28:29","modified_gmt":"2009-01-29T12:28:29","slug":"sneaky-aliases","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/01\/29\/sneaky-aliases\/","title":{"rendered":"Sneaky Aliases"},"content":{"rendered":"<p>I love aliases in PowerShell because they make working in the interactive shell so much easier and faster. I&#8217;ve even created aliases for common apps I use like PrimalScript and PrimalForms so I can launch them from PowerShell with a few keystrokes. One drawback to aliases is that you can use them to reference any command that takes parameters. The alias can only substitute for the command. That means I can&#8217;t do this:<\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas;\">PS C:\\ new-alias -name t -value (get-date -displayhint time)<\/span><\/p>\n<p>PowerShell will complain. But there is a sneaky work around. You can alias a function.\u00a0 All I need to do is wrap the expression in a function and create an alias to that.<\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas;\">PS C:\\&gt; function show-time {get-date -displayhint time}<br \/>\nPS C:\\&gt; new-alias t show-time<br \/>\nPS C:\\&gt; t <\/span><\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas;\">11:14:36 AM <\/span><\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas;\">PS C:\\&gt;<\/span><\/p>\n<p>I can add these lines to my profile and presto, I have the alias I want. As you can see, PowerShell functions don&#8217;t have to be complicated or long.\u00a0 In fact I didn&#8217;t even have to create a script. You can use this technique in PowerShell v1.0 and the latest PowerShell CTP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>love aliases in PowerShell because they make working in the interactive shell so much easier and faster. I&#8217;ve even created aliases for common apps I use like PrimalScript and PrimalForms so I can launch them from PowerShell with a few keystrokes. One drawback to aliases is that you can use them to reference any command that takes parameters. The alias can only substitute for the command. But there is a sneaky work around.<\/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":[283,25],"tags":[444,198,28,445],"class_list":["post-1106","post","type-post","status-publish","format-standard","hentry","category-howto","category-windows-powershell","tag-alias","tag-function","tag-powershell","tag-profile"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1106","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=1106"}],"version-history":[{"count":4,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1106\/revisions"}],"predecessor-version":[{"id":1107,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1106\/revisions\/1107"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}