{"id":8791,"date":"2007-01-11T23:11:00","date_gmt":"2007-01-11T23:11:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2007\/01\/11\/set-alias-strict\/"},"modified":"2019-02-18T13:20:53","modified_gmt":"2019-02-18T20:20:53","slug":"set-alias-strict","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/set-alias-strict\/","title":{"rendered":"Set-Alias-Strict"},"content":{"rendered":"<p><!--\n \/* Font Definitions *\/\n @font-face\n\t{font-family:\"Cambria Math\";\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\n@font-face\n\t{font-family:Cambria;\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\n@font-face\n\t{font-family:Calibri;\n\tpanose-1:2 15 5 2 2 2 4 3 2 4;}\n@font-face\n\t{font-family:\"Lucida Console\";\n\tpanose-1:2 11 6 9 4 5 4 2 2 4;}\n \/* Style Definitions *\/\n p.MsoNormal, li.MsoNormal, div.MsoNormal\n\t{margin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nh2\n\t{mso-style-link:\"Heading 2 Char\";\n\tmargin-top:10.0pt;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.75in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tpage-break-after:avoid;\n\tfont-size:13.0pt;\n\tfont-family:\"Cambria\",\"serif\";\n\tcolor:#4F81BD;\n\tfont-weight:bold;}\na:link, span.MsoHyperlink\n\t{color:blue;\n\ttext-decoration:underline;}\na:visited, span.MsoHyperlinkFollowed\n\t{color:purple;\n\ttext-decoration:underline;}\np\n\t{margin-right:0in;\n\tmargin-left:0in;\n\tfont-size:12.0pt;\n\tfont-family:\"Times New Roman\",\"serif\";}\np.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph\n\t{margin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.5in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nspan.Heading2Char\n\t{mso-style-name:\"Heading 2 Char\";\n\tmso-style-link:\"Heading 2\";\n\tfont-family:\"Cambria\",\"serif\";\n\tcolor:#4F81BD;\n\tfont-weight:bold;}\np.msolistparagraphcxspfirst, li.msolistparagraphcxspfirst, div.msolistparagraphcxspfirst\n\t{mso-style-name:msolistparagraphcxspfirst;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.5in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\np.msolistparagraphcxspmiddle, li.msolistparagraphcxspmiddle, div.msolistparagraphcxspmiddle\n\t{mso-style-name:msolistparagraphcxspmiddle;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.5in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\np.msolistparagraphcxsplast, li.msolistparagraphcxsplast, div.msolistparagraphcxsplast\n\t{mso-style-name:msolistparagraphcxsplast;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.5in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nspan.CodeChar\n\t{mso-style-name:\"Code Char\";\n\tmso-style-link:Code;\n\tfont-family:\"Lucida Console\";\n\tcolor:#EEEDF0;\n\tbackground:#012456;}\np.Code, li.Code, div.Code\n\t{mso-style-name:Code;\n\tmso-style-link:\"Code Char\";\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:12.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tbackground:#012456;\n\tfont-size:8.0pt;\n\tfont-family:\"Lucida Console\";\n\tcolor:#EEEDF0;}\np.msopapdefault, li.msopapdefault, div.msopapdefault\n\t{mso-style-name:msopapdefault;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tfont-size:12.0pt;\n\tfont-family:\"Times New Roman\",\"serif\";}\n.MsoChpDefault\n\t{font-size:10.0pt;}\n@page Section1\n\t{size:8.5in 11.0in;\n\tmargin:1.0in 1.0in 1.0in 1.0in;}\ndiv.Section1\n\t{page:Section1;}\n--><\/p>\n<p>I picked up Jerry Lee Ford&#8217;s book <a href=\"http:\/\/www.amazon.com\/gp\/product\/1598633546\/ref=pd_cp_b_title\/002-9954223-4981632\">Microsoft Windows PowerShell Programming for the absolute beginner<\/a> .&nbsp; <\/p>\n<p>In the section on aliases, he warns users that Set-Alias does not verify the validity of the alias assignment.&nbsp; That is a good point.&nbsp; Here is a function that does that for you.&nbsp; <\/p>\n<p class=\"Code\"><span>function Set-Alias-Strict($Name, $Value)<br \/>{<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>$null = Get-Command $Value<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Set-Alias $Name $Value<span>&nbsp;&nbsp;&nbsp; <\/span><br \/>} <br \/>Set-Alias sals Set-Alias-Strict<\/span><\/p>\n<p>Here is that function in action:<\/p>\n<p class=\"Code\">PS&gt; Set-Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>test Get-Porcess<br \/>PS&gt; Set-Alias-Strict test Get-Porcess<br \/>Get-Command : The term &#8216;Get-Porcess&#8217; is not recognized as a cmdlet, functio<br \/>n, operable program, or script file. Verify the term and try again.<br \/>At line:3 char:24<br \/>+<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$null = Get-Command<span>&nbsp; <\/span>&lt;&lt;&lt;&lt; $Value<br \/>PS&gt; Set-Alias-Strict test Get-Process<br \/>PS&gt;<\/p>\n<p><br><\/p>\n<p>&nbsp;I transpose characters on a fairly frequent basis so this is a good thing for me to use.&nbsp; Of&nbsp;course I don&#8217;t want to type all that so the alias comes in handy:<\/p>\n<p class=\"Code\">PS&gt; sals test Get-Porcess<br \/>Get-Command : The term &#8216;Get-Porcess&#8217; is not recognized as a cmdlet, functio<br \/>n, operable program, or script file. Verify the term and try again.<br \/>At line:3 char:24<br \/>+<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$null = Get-Command<span>&nbsp; <\/span>&lt;&lt;&lt;&lt; $Value<br \/>PS&gt; sals test Get-Process<br \/>PS&gt;<\/p>\n<p>Notice the naming model&nbsp;I used.&nbsp;&nbsp;If&nbsp;there is a command that doesn&#8217;t have the functions you want it to have, I recommend the naming scheme here (this was first suggested by Lee Holmes).&nbsp; Create a new name by taking the old name and concatenating a &#8220;-&#8221;&nbsp;and a word.&nbsp;&nbsp;(e.g. &#8220;-Strict&#8221;&nbsp;).&nbsp; This makes it easier to find and understand.<\/p>\n<p class=\"Code\">PS&gt; gcm *-alias*<\/p>\n<p>CommandType<span>&nbsp;&nbsp;&nbsp;&nbsp; Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Definition<br \/>&#8212;&#8212;&#8212;&#8211;<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>&#8212;&#8212;&#8212;-<br \/>Cmdlet<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Export-Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Export-Alias [-Path] &lt;Stri&#8230;<br \/>Cmdlet<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Get-Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Get-Alias [[-Name] &lt;String&#8230;<br \/>Cmdlet<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Import-Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Import-Alias [-Path] &lt;Stri&#8230;<br \/>Cmdlet<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>New-Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>New-Alias [-Name] &lt;String&gt;&#8230;<br \/>Cmdlet<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Set-Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Set-Alias [-Name] &lt;String&gt;&#8230;<br \/>Function<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Set-Alias-Strict<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>param($Name, $Value) $null&#8230;<\/p>\n<p>PS&gt; <\/p>\n<p><br><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Enjoy!&nbsp;<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/MMC Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I picked up Jerry Lee Ford&#8217;s book Microsoft Windows PowerShell Programming for the absolute beginner .&nbsp; In the section on aliases, he warns users that Set-Alias does not verify the validity of the alias assignment.&nbsp; That is a good point.&nbsp; Here is a function that does that for you.&nbsp; function Set-Alias-Strict($Name, $Value){&nbsp;&nbsp;&nbsp; $null = Get-Command [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>I picked up Jerry Lee Ford&#8217;s book Microsoft Windows PowerShell Programming for the absolute beginner .&nbsp; In the section on aliases, he warns users that Set-Alias does not verify the validity of the alias assignment.&nbsp; That is a good point.&nbsp; Here is a function that does that for you.&nbsp; function Set-Alias-Strict($Name, $Value){&nbsp;&nbsp;&nbsp; $null = Get-Command [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=8791"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8791\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=8791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=8791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=8791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}