{"id":13581,"date":"2011-06-19T00:01:00","date_gmt":"2011-06-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/06\/19\/top-ten-favorite-powershell-tricks-part-2\/"},"modified":"2011-06-19T00:01:00","modified_gmt":"2011-06-19T00:01:00","slug":"top-ten-favorite-powershell-tricks-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/top-ten-favorite-powershell-tricks-part-2\/","title":{"rendered":"Top Ten Favorite PowerShell Tricks: Part 2"},"content":{"rendered":"<h2><b>Learn Top Tricks for Quickly Learning Windows PowerShell<\/b><\/h2>\n<p><b>Summary<\/b>: The Microsoft Scripting Guy shares more of his top 10 tricks for working effectively with Windows PowerShell.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The weather in Charlotte, North Carolina quickly changed from nice, pleasant spring to hot and sticky summer. It seems that spring lasted for about a week&mdash;now it is summer. I am not a &ldquo;get outside and swelter in the hot humidity&rdquo; kind of a Scripting Guy. I am more of a find a nice cool spot for my laptop kind of Scripting Guy. Anyway, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/18\/top-ten-favorite-powershell-tricks-part-1.aspx\" target=\"_blank\">yesterday<\/a> I began my top ten list of favorite Windows PowerShell tricks.&nbsp;Today I will continue the top ten list with Scripting Guy favorite PowerShell Tricks 5, 6, and 7. See tommorow&#8217;s post for <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/20\/top-ten-favorite-powershell-tricks-part-3-working-with-ranges-dates-and-other-cool-powershell-tricks.aspx\">tricks 8-10<\/a>.<\/p>\n<h2><b>Weekend Scripter<\/b><\/h2>\n<h3>Trick #5: Use square brackets to index into an array<\/h3>\n<p>Many times Windows PowerShell cmdlets return a collection, or an array of objects. To work with collections or arrays it is often common to send the collection over the pipeline, or to store the information in a variable and use the <b>ForEach<\/b> statement to iterate through the collection. Inside the pipeline (or loop), when one has an individual object, one can then examine its members. Often when I am experimenting or exploring from the Windows PowerShell console, I do not want to take the time to set up a loop to work with the object. Instead I will index directly into the array by using square brackets. An array begins numbering at 0 in Windows PowerShell, and therefore I can always use [0] to retrieve the first record in the array (or collection). I can even combine this technique with my grouping trick from yesterday&hellip;use a pair of parentheses to force evaluation of the command. This technique is illustrated here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; (Get-Service)[0]<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Status<span>&nbsp;&nbsp; <\/span>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>DisplayName<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&#8212;&#8212;<span>&nbsp;&nbsp; <\/span>&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8211;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Stopped<span>&nbsp; <\/span>AdtAgent<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Operations Manager Audit Forwarding&#8230;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; (Get-EventLog -LogName application)[0]<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>Index Time<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>EntryType<span>&nbsp;&nbsp; <\/span>Source<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>InstanceID Message<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>&#8212;&#8211; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;<span>&nbsp;&nbsp; <\/span>&#8212;&#8212;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;- &#8212;&#8212;-<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp; <\/span><\/span><span>118582 May 14 13:17<span>&nbsp; <\/span>Information Office Software P&#8230;<span>&nbsp;&nbsp; <\/span>1073742827 The Software Protection ser&#8230;<\/span><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>You will notice that the event log command takes a long time to return the first record. This is because the command syntax says to get all of the records from the application log and then index them into the first record. This is a very inefficient way to accomplish this task. It is better to use the <i>Newest <\/i>parameter as shown in the following command.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; Get-EventLog -LogName application -Newest 1<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>Index Time<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>EntryType<span>&nbsp;&nbsp; <\/span>Source<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>InstanceID Message<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>&#8212;&#8211; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;<span>&nbsp;&nbsp; <\/span>&#8212;&#8212;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;- &#8212;&#8212;-<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp; <\/span><\/span><span>118582 May 14 13:17<span>&nbsp; <\/span>Information Office Software P&#8230;<span>&nbsp;&nbsp; <\/span>1073742827 The Software Protection ser&#8230;<\/span><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<h3>Trick #6: Use tab expansion<\/h3>\n<p>One of the great benefits of working interactively in the Windows PowerShell console is tab expansion. During the 2011 Scripting Games, we discouraged the use of aliases because of the emphasis on scripts. But when working interactively in the Windows PowerShell console, the use of aliases is a great benefit because it makes things go so much quicker. But one of the reasons I am experimenting in the first place is because I will one day incorporate what I learn into a script.<\/p>\n<p>I wrote a Windows PowerShell ISE add-in that will replace Windows PowerShell aliases with the actual Windows PowerShell cmdlet name in a script, but that is still an extra step. Besides, when I use the <b>History<\/b> command, at times all the aliases get a bit confusing. To mitigate this, I use tab expansion extensively. In this way, I get the benefit of quick typing, and I have the added benefit that if I copy the command to the Windows PowerShell ISE, I do not need to translate it&mdash;the command is complete and ready to go.<\/p>\n<p>Therefore, if I want to use the <b>Get-Process<\/b> cmdlet, there is an alias, <b>gps<\/b>. But I can also type <b>Get-p<\/b> and press the &lt;tab&gt; key, and it will complete the command. If there are several matches (as is the case with <b>Get-p<\/b>), I simply press the &lt;tab&gt; key again and again until the command I wish to use appears. If I accidently go too far (i.e., I pressed &lt;tab&gt; too fast and skipped past the command I wanted), I can hold down the &lt;shift&gt; key and press &lt;tab&gt;, and it returns to the previous command.<\/p>\n<p>The really cool thing about tab expansion is that it works for parameter names as well. For clarity, I advocate always using full parameter names in scripts, and quite often, I will include them when working interactively in the Windows PowerShell console (they are often required depending on the parameter set).<\/p>\n<p>Adding parameter names is really easy. After the Windows PowerShell cmdlet name (or alias) is in place, I type a dash (-) and press the &lt;tab&gt; key to bring up the first parameter name. I continue pressing &lt;tab&gt; until I find the parameter I want to use.<\/p>\n<p>The big reason I might choose to leave out parameter names, or use aliases extensively, is when the Windows PowerShell command is growing to the point it consumes multiple lines in the Windows PowerShell console. At this point, the command becomes difficult to work with and I therefore use aliases to shorten the command.<\/p>\n<p>You should learn a few aliases (especially the one letter ones) so you will become more productive in the Windows PowerShell console. To see all of the one-letter aliases, use the following command.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; Get-Alias ?<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Here is the command output:&nbsp;<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Definition<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&#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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;-<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Alias<span>&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>%<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>ForEach-Object<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Alias<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;&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>Where-Object<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>h<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Get-History<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>i<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;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Invoke-History<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Alias<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>r<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Invoke-History<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>(<strong>Note<\/strong>: The <b>i <\/b>alias is one that I created and placed in my Windows PowerShell profile. For more information about aliases, refer to <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=alias&amp;sections=7618\" target=\"_blank\">this series of Hey! Scripting Guy blogs<\/a>. For more information about Windows PowerShell profiles, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/getting+started\/profiles\/\" target=\"_blank\">this series of Hey! Scripting Guy blogs<\/a>)<\/p>\n<p>If you are reading a blog somewhere, and you see an alias that you are not sure about, you can look that alias up by using the <b>Get-Alias<\/b> cmdlet. This is illustrated here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; Get-Alias %<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Definition<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&#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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;-<\/span><\/span><\/span><\/p>\n<p><span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Alias<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;&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>ForEach-Object<\/span><\/span><\/span><\/p><\/blockquote>\n<p>Because the question mark is a wildcard character (for a single character), the <b>Get-Alias<\/b> cmdlet interprets its use as a wildcard; and therefore, it returns the one letter aliases. If I want to look up the meaning of the <b>?<\/b> alias, I need to escape the <b>?<\/b> character with the back tick. This is shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; Get-Alias -Name &#8216;`?&#8217;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Definition<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&#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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;-<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Alias<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;&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>Where-Object<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>If you really like using tab expansion, you can enhance its ability by modifying the <b>TabExpansion<\/b> function. A better bet is to use <b>PowerTab<\/b>, the enhanced tab expansion function from <a href=\"http:\/\/powertab.codeplex.com\/\" target=\"_blank\">CodePlex<\/a>.<\/p>\n<h3>Trick #7: Use &ndash;list to easily find WMI classes<\/h3>\n<p>Most people know that they can easily produce a list of WMI classes. This is illustrated in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6837.hsg-06-19-11-01_5309EE7A.png\"><img decoding=\"async\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" title=\"Image of command output\" border=\"0\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6443.hsg-06-19-11-01_thumb_44CB758A.png\" width=\"604\" height=\"424\" \/><\/a><\/p>\n<p>If I need to find WMI classes that are related to memory, I add *memory* just after the <i>List<\/i> parameter. This will filter out only WMI classes that contain the word <b>Memory <\/b>within the class name. This is illustrated in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3704.hsg-06-19-11-02_445F4295.png\"><img decoding=\"async\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" title=\"Image of command output\" border=\"0\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1072.hsg-06-19-11-02_thumb_563BD662.png\" width=\"604\" height=\"424\" \/><\/a><\/p>\n<p>Most of the time, I do not mind weeding through the list of WMI class names that are returned. However, at times, I am looking for something rather specific. For example, suppose I am looking for formatted performance counter classes that are related to memory. To find this, I add a little bit to my wildcard filter. The command I came up with is shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Get-WmiObject -List win32*perfFormatted*memory*<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The command and its associated output appear in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5367.hsg-06-19-11-03_31AA48DE.png\"><img decoding=\"async\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" title=\"Image of command output\" border=\"0\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4784.hsg-06-19-11-03_thumb_2A8B0C66.png\" width=\"604\" height=\"286\" \/><\/a><\/p>\n<p>As you can see, the complete class names are truncated. I get around that by using the <b>Select-Object<\/b> cmdlet as shown here (<b>Select<\/b> is an alias for the <b>Select-Object<\/b> cmdlet).<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">PS C:\\&gt; Get-WmiObject -List win32*perfFormatted*memory* | select name<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Name<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&#8212;-<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Win32_PerfFormattedData_MSSQLSQLEXPRESS_MSSQLSQLEXPRESSMemoryManager<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Win32_PerfFormattedData_NETFramework_NETCLRMemory<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Win32_PerfFormattedData_NETMemoryCache40_NETMemoryCache40<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Win32_PerfFormattedData_PerfOS_Memory<\/span><\/span><\/span><span><\/span><\/p>\n<\/blockquote>\n<p>I will continue my top ten list of Windows PowerShell tricks <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/20\/top-ten-favorite-powershell-tricks-part-3-working-with-ranges-dates-and-other-cool-powershell-tricks.aspx\">tomorrow, when I reveal favorite tricks 8-10<\/a>.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">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 href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">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>Learn Top Tricks for Quickly Learning Windows PowerShell Summary: The Microsoft Scripting Guy shares more of his top 10 tricks for working effectively with Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. The weather in Charlotte, North Carolina quickly changed from nice, pleasant spring to hot and sticky summer. It seems that spring lasted [&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":[51,3,4,45],"class_list":["post-13581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Learn Top Tricks for Quickly Learning Windows PowerShell Summary: The Microsoft Scripting Guy shares more of his top 10 tricks for working effectively with Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. The weather in Charlotte, North Carolina quickly changed from nice, pleasant spring to hot and sticky summer. It seems that spring lasted [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13581","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=13581"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13581\/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=13581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}