Set-Alias-Strict

Set-Alias-Strict

  • Comments 3

I picked up Jerry Lee Ford's book Microsoft Windows PowerShell Programming for the absolute beginner

In the section on aliases, he warns users that Set-Alias does not verify the validity of the alias assignment.  That is a good point.  Here is a function that does that for you. 

function Set-Alias-Strict($Name, $Value)
{
    $null = Get-Command $Value
    Set-Alias $Name $Value   
}
Set-Alias sals Set-Alias-Strict



Here is that function in action:

PS> Set-Alias        test Get-Porcess
PS> Set-Alias-Strict test Get-Porcess
Get-Command : The term 'Get-Porcess' is not recognized as a cmdlet, functio
n, operable program, or script file. Verify the term and try again.
At line:3 char:24
+     $null = Get-Command  <<<< $Value
PS> Set-Alias-Strict test Get-Process
PS>


 I transpose characters on a fairly frequent basis so this is a good thing for me to use.  Of course I don't want to type all that so the alias comes in handy:

PS> sals test Get-Porcess
Get-Command : The term 'Get-Porcess' is not recognized as a cmdlet, functio
n, operable program, or script file. Verify the term and try again.
At line:3 char:24
+     $null = Get-Command  <<<< $Value
PS> sals test Get-Process
PS>

Notice the naming model I used.  If there is a command that doesn't have the functions you want it to have, I recommend the naming scheme here (this was first suggested by Lee Holmes).  Create a new name by taking the old name and concatenating a "-" and a word.  (e.g. "-Strict" ).  This makes it easier to find and understand.

PS> gcm *-alias*

CommandType     Name                          Definition
-----------     ----                          ----------
Cmdlet          Export-Alias                  Export-Alias [-Path] <Stri...
Cmdlet          Get-Alias                     Get-Alias [[-Name] <String...
Cmdlet          Import-Alias                  Import-Alias [-Path] <Stri...
Cmdlet          New-Alias                     New-Alias [-Name] <String>...
Cmdlet          Set-Alias                     Set-Alias [-Name] <String>...
Function        Set-Alias-Strict              param($Name, $Value) $null...

PS>


 

Enjoy! 

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

 

Leave a Comment
  • Please add 3 and 3 and type the answer here:
  • Post
  • This is not a comment to this post, but it is to your blog in general. I really like reading your blog and have done so for a long time now, but since I mostly read blogs with the RSS-reader in IE7 the styling of the code samples makes the posts harder to read, for some reason IE7 (at least the version I'm using Vista RC1) dispays the styling as a big chunk of text and the post starts right after that without an extra linebrake to seperate the content from the styling.

  • with the verb-noun sytax strongly enforced for cmdlets, can the noun have a dash in it? will this compile or does the noun have to be just alpha or alpha numeric.. if a cmdlet noun can have a dash in it , then i can support this naming convention but if this only works for functions and not cmdlets, then i think it should be revised.

  • When I saw Kjartan's comment I had a look at the blog in IE 7.  (I usually use FeedDemon.)

    This is IE 7 on Windows 2003, everything patched up to date.

    In IE I got a lot of garbage at the article head.  This looks like Office generated drek and I'll attempt to paste it below.

    "p.MsoNormal, li.MsoNormal, div.MsoNormal {margin-top:0in;margin-right:0in;margin-bottom:10.0pt;margin-left:.75in;line-height:115%;font-size:11.0pt;font-family:'Calibri","sans-serif';} h2 {margin-top:10.0pt;margin-right:0in;margin-bottom:0in;margin-left:.75in;margin-bottom:.0001pt;line-height:115%;page-break-after:avoid;font-size:13.0pt;font-family:'Cambria","serif';color:#4F81BD;font-weight:bold;} a:link, span.MsoHyperlink {color:blue;text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {color:purple;text-decoration:underline;} p {margin-right:0in;margin-left:0in;font-size:12.0pt;font-family:'Times New Roman","serif';} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {margin-top:0in;margin-right:0in;margin-bottom:10.0pt;margin-left:.5in;line-height:115%;font-size:11.0pt;font-family:'Calibri","sans-serif';} span.Heading2Char {font-family:'Cambria","serif';color:#4F81BD;font-weight:bold;} p.msolistparagraphcxspfirst, li.msolistparagraphcxspfirst, div.msolistparagraphcxspfirst {margin-top:0in;margin-right:0in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt;line-height:115%;font-size:11.0pt;font-family:'Calibri","sans-serif';} p.msolistparagraphcxspmiddle, li.msolistparagraphcxspmiddle, div.msolistparagraphcxspmiddle {margin-top:0in;margin-right:0in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt;line-height:115%;font-size:11.0pt;font-family:'Calibri","sans-serif';} p.msolistparagraphcxsplast, li.msolistparagraphcxsplast, div.msolistparagraphcxsplast {margin-top:0in;margin-right:0in;margin-bottom:10.0pt;margin-left:.5in;line-height:115%;font-size:11.0pt;font-family:'Calibri","sans-serif';} span.CodeChar {font-family:'Lucida Console';color:#EEEDF0;background:#012456;} p.Code, li.Code, div.Code {margin-top:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:.75in;line-height:115%;background:#012456;font-size:8.0pt;font-family:'Lucida Console';color:#EEEDF0;} p.msopapdefault, li.msopapdefault, div.msopapdefault {margin-top:0in;margin-right:0in;margin-bottom:10.0pt;margin-left:.75in;line-height:115%;font-size:12.0pt;font-family:'Times New Roman","serif';} .MsoChpDefault {font-size:10.0pt;} @page Section1 {size:8.5in 11.0in;margin:1.0in 1.0in 1.0in 1.0in;} div.Section1 {page:Section1;}

    I picked up Jerry Lee Ford's book Microsoft Windows PowerShell Programming for the absolute beginner .  

    "

    That seems to have pasted OK.

    Other posts, but not all, have similar rubbish.  Hope that helps.

Page 1 of 1 (3 items)