{"id":14521,"date":"2011-04-16T00:01:00","date_gmt":"2011-04-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/04\/16\/create-a-hash-table-with-powershell-aliases-and-definitions\/"},"modified":"2011-04-16T00:01:00","modified_gmt":"2011-04-16T00:01:00","slug":"create-a-hash-table-with-powershell-aliases-and-definitions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-a-hash-table-with-powershell-aliases-and-definitions\/","title":{"rendered":"Create a Hash Table with PowerShell Aliases and Definitions"},"content":{"rendered":"<p><b>Summary<\/b>: In preparation for creating a Windows PowerShell ISE add-on to automatically remove aliases, Scripting Guy Ed Wilson builds a hash table with alias definitions.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, here. It has finally happened, I unveiled the last event of the 2011 Windows PowerShell Scripting Games on Friday. Contestants still have a week to deliver their solutions for the remaining events, but the games are drawing to a conclusion. <\/p>\n<p>The judging criteria for the 2011 Scripting Games are not arbitrary, but they actually represent best practices to incorporate when writing enterprise production scripts. One of those guidelines is to not use Windows PowerShell cmdlet aliases in a script. This causes a few problems for people, because when working interactively from the Windows PowerShell console, the use of aliases is encouraged because it simplifies typing and allows one to get the task accomplished more quickly. <\/p>\n<p>Because I use the Windows PowerShell console for prototyping commands prior to incorporating them into the Windows PowerShell ISE, I often copy code from the console and paste it into the Windows PowerShell ISE. This causes a dilemma&mdash;I like to use aliases in the Windows PowerShell console, but in a script, I should not use aliases. Therefore, when prototyping a command, I have two choices: do not use aliases, or rewrite all my code after I paste it into the Windows PowerShell ISE. Neither choice is great, but until recently those were my two options. <\/p>\n<p>While I was grading some of the scripts for the 2011 Scripting Games, a third option to the problem of aliases came to me: Write a script and have it replace all aliases with the full cmdlet names. <\/p>\n<p>Today&rsquo;s blog is the first of a series of four that will discuss the parts of my solution to the problem of working with aliases. In this blog, I want to create a hash table and populate it with all of the available aliases. The code that accomplishes this task is shown here.<\/p>\n<blockquote>\n<h5>New-HashTableWithAliases.ps1<\/h5>\n<\/blockquote>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Get-Alias |<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 9pt\">&nbsp;<\/span><\/span><span style=\"font-size: 9pt\">Select-object name, definition | <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 9pt\">&nbsp;<\/span><\/span><span style=\"font-size: 9pt\">Foreach-object -begin {$a = @{} } `<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 9pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 9pt\">-process { $a.add($_.name,$_.definition)} `<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 9pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 9pt\">-end {$a}<\/span><\/span><\/span><\/span><span style=\"font-family:\"><\/span><\/p>\n<\/blockquote>\n<p>The <b>Get-Alias<\/b> cmdlet returns a list of aliases and their associated definitions. Each alias that is returned by this cmdlet is an instance of an <b>aliasinfo<\/b> object. The members of that object are shown here.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">PS C:\\&gt; Get-Alias | Get-Member<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: 9pt\">TypeName: System.Management.Automation.AliasInfo<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>MemberType<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Definition<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;-<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Equals<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>bool Equals(System.Object obj)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">GetHashCode<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>int GetHashCode()<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">GetType<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>type GetType()<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">ToString<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>string ToString()<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">CommandType<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.Automation.CommandTypes Comm&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Definition<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.String Definition {get;}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Description<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.String Description {get;set;}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Module<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.Automation.PSModuleInfo Modu&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">ModuleName<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.String ModuleName {get;}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.String Name {get;}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Options<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.Automation.ScopedItemOptions&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">OutputType<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Collections.ObjectModel.ReadOnlyCollect&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Parameters<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Collections.Generic.Dictionary`2[[Syste&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">ParameterSets<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Collections.ObjectModel.ReadOnlyCollect&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">ReferencedCommand<span>&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.Automation.CommandInfo Refer&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">ResolvedCommand<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.Automation.CommandInfo Resol&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Visibility<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.Automation.SessionStateEntry&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">HelpUri<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>ScriptProperty System.Object HelpUri {get=try&#8230;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">ResolvedCommandName ScriptProperty System.Object ResolvedCommandName {get=$this.R&#8230;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>There are two properties from the <b>aliasinfo<\/b> object that I want to retrieve: the <b>name<\/b> and the <b>definition<\/b>. The <b>name<\/b> property is the actual alias itself; the <b>definition<\/b> property is the full cmdlet name that resolves to the alias. The following code illustrates this concept by using the <b>Get-Alias<\/b> cmdlet to retrieve the alias named <b>gps<\/b>. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">PS C:\\&gt; Get-Alias gps<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">CommandType<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Definition<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">&#8212;&#8212;&#8212;&#8211;<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;-<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;-<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>gps<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Get-Process<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>To retrieve only the <b>name<\/b> and the <b>definition<\/b> from all of the aliases, I use the code seen here.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">Get-Alias |<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 9pt\">&nbsp;<\/span><\/span><span style=\"font-size: 9pt\">Select-Object name, definition<\/span><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The command to retrieve aliases and definitions and the associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3806.HSG-4-16-11-01_3DC95D06.jpg\"><img decoding=\"async\" height=\"366\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1667.HSG-4-16-11-01_thumb_4CD01613.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>When I have the aliases and their associated definition, I create a hash table for storage. The name will be the key property because all aliases have to be unique. The definition will be the value that is associated with the key because there could be several aliases for a single cmdlet. <\/p>\n<p>To create an empty hash table, I use the syntax shown here where I store the empty hash table in the <b>$a<\/b> variable<i>. <\/i>I only need to perform this action once, so I place this command in the begin portion of the <b>Foreach-Object<\/b> cmdlet. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">-begin {$a = @{} }}<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>To add to the hash table, I use the <b>add<\/b> method. The first item is the key, and the second item is the value. In this example, I am adding the name as the key and the definition as the value. I will add each alias and its associated definition as they flow through the <b>Foreach-Object<\/b> cmdlet. Therefore, this section goes into the process portion of the command. Here is that portion of the script. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">-process { $a.add($_.name,$_.definition)}<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The last thing I do here, is to display the contents of the hash table that is stored in the <b>$a<\/b> variable. This action will occur after everything else has completed; and therefore, it appears in the end portion of the <b>ForEach-Object<\/b> cmdlet.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 9pt\">-end {$a}<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The script and its associated output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1777.HSG-4-16-11-02_5A067959.jpg\"><img decoding=\"async\" height=\"422\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8551.HSG-4-16-11-02_thumb_53434A09.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In preparation for creating a Windows PowerShell ISE add-on to automatically remove aliases, Scripting Guy Ed Wilson builds a hash table with alias definitions. Microsoft Scripting Guy, Ed Wilson, here. It has finally happened, I unveiled the last event of the 2011 Windows PowerShell Scripting Games on Friday. Contestants still have a week to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[18,3,4,45],"class_list":["post-14521","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hash-tables-and-dictionary-objects","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: In preparation for creating a Windows PowerShell ISE add-on to automatically remove aliases, Scripting Guy Ed Wilson builds a hash table with alias definitions. Microsoft Scripting Guy, Ed Wilson, here. It has finally happened, I unveiled the last event of the 2011 Windows PowerShell Scripting Games on Friday. Contestants still have a week to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/14521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=14521"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/14521\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=14521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=14521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=14521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}