<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Andrew Arnott : PowerShell</title><link>http://blogs.msdn.com/andrewarnottms/archive/tags/PowerShell/default.aspx</link><description>Tags: PowerShell</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How to sort the console output of a hashtable in PowerShell</title><link>http://blogs.msdn.com/andrewarnottms/archive/2007/07/10/how-to-sort-the-console-output-of-a-hashtable-in-powershell.aspx</link><pubDate>Tue, 10 Jul 2007 02:01:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3787618</guid><dc:creator>andarno</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/andrewarnottms/comments/3787618.aspx</comments><wfw:commentRss>http://blogs.msdn.com/andrewarnottms/commentrss.aspx?PostID=3787618</wfw:commentRss><description>&lt;p&gt;Hashtables are inherently unsorted, but when you're printing&amp;nbsp;a hashtable's contents to the console, it can certainly be helpful to sort its contents by key.&amp;nbsp; Although it's not obvious, the way to do it is pretty easy.&lt;/p&gt; &lt;p&gt;Let's start with defining a hashtable and play with it until it's obviously unsorted.&lt;/p&gt;&lt;pre class="command"&gt;PS C:\&amp;gt; $h = @{b=2;a=3;c=1}
PS C:\&amp;gt; $h

Name                           Value
----                           -----
a                              3
b                              2
c                              1


PS C:\&amp;gt; $h['d']=5
PS C:\&amp;gt; $h

Name                           Value
----                           -----
a                              3
b                              2
d                              5
c                              1
&lt;/pre&gt;
&lt;p&gt;Great. Now let's sort it:&lt;/p&gt;&lt;pre class="command"&gt;PS C:\&amp;gt; $h | sort name

Name                           Value
----                           -----
a                              3
b                              2
d                              5
c                              1
&lt;/pre&gt;
&lt;p&gt;Ergh! What happened? Well, without going into tons of detail, it has to do with the type that backs HashTable and how you can't sort the object itself. You've got to generate a list of key-value pairs and sort that.&lt;/p&gt;
&lt;p&gt;Here's the correct way to do it:&lt;/p&gt;&lt;pre class="command"&gt;PS C:\&amp;gt; $h.GetEnumerator() | sort name

Name                           Value
----                           -----
a                              3
b                              2
c                              1
d                              5
&lt;/pre&gt;
&lt;p&gt;Great. And yes, sorting by value works too:&lt;/p&gt;&lt;pre class="command"&gt;PS C:\&amp;gt; $h.GetEnumerator() | sort value

Name                           Value
----                           -----
c                              1
b                              2
a                              3
d                              5
&lt;/pre&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3787618" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/andrewarnottms/archive/tags/PowerShell/default.aspx">PowerShell</category></item><item><title>Getting doskey macros to work in PowerShell</title><link>http://blogs.msdn.com/andrewarnottms/archive/2007/07/09/getting-doskey-macros-to-work-in-powershell.aspx</link><pubDate>Tue, 10 Jul 2007 01:17:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3787243</guid><dc:creator>andarno</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/andrewarnottms/comments/3787243.aspx</comments><wfw:commentRss>http://blogs.msdn.com/andrewarnottms/commentrss.aspx?PostID=3787243</wfw:commentRss><description>&lt;p&gt;As much as I love to work with PowerShell, it was hard to give up the doskey macros I had defined for CMD.exe.&amp;nbsp; But I just found out that doskey can work all its magic for PowerShell too, so here's the trick...&lt;/p&gt; &lt;p&gt;Doskey defaults to working only with cmd.exe, but by passing the /exename= parameter to it, you can set macros for PowerShell as well.&amp;nbsp; For example:&lt;/p&gt; &lt;p class="command"&gt;PS&amp;gt; doskey /exename=powershell.exe cd\home=pushd $env:USERPROFILE\$*&lt;/p&gt; &lt;p&gt;This effectively commandeers any command-line instruction beginning with "cd\home" and expands it to "$env:USERPROFILE\", appending any 'parameters' after "cd\home" to the end.&amp;nbsp; &lt;/p&gt; &lt;p&gt;If you already have a text file of macros defined that you're used to reading in using:&lt;/p&gt; &lt;p class="command"&gt;CMD.exe C:\&amp;gt; doskey /MACROFILE=mymacros.txt&lt;/p&gt; &lt;p&gt;The following syntax should work:&lt;/p&gt; &lt;p class="command"&gt;PS&amp;gt; doskey /exename=powershell.exe /MACROFILE=mymacros.txt&amp;nbsp; # Slow down there, Turbo!&lt;/p&gt; &lt;p&gt;You shouldn't run and add the /exename=powershell.exe parameter just yet.&amp;nbsp; You should go through your macros.txt file and review it for any syntax changes you need to make for PowerShell.&amp;nbsp; For example, your macros.txt file may contain the following line:&lt;/p&gt; &lt;p&gt;cd\home=%USERPROFILE%\$*&lt;/p&gt; &lt;p&gt;But this line will need to be changed to&lt;/p&gt; &lt;p&gt;cd\home=$env:USERPROFILE\$*&lt;/p&gt; &lt;p&gt;Once you've converted your cmd.exe syntax to Windows PowerShell syntax, you should be good to go.&lt;/p&gt; &lt;p&gt;On more thing.&amp;nbsp; Some of you may be thinking "sure you can use doskey macros, but you &lt;em&gt;should&lt;/em&gt; take advantage of PowerShell's unique alias and function facilities."&amp;nbsp; And you'd be right -- except that doskey macros allows for some macro names that PowerShell aliases and functions do not allow.&amp;nbsp; For example, try writing a function or alias named "cd\home" in PowerShell.&amp;nbsp; Of course you can't. But doskey macros make it work even in PowerShell.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3787243" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/andrewarnottms/archive/tags/PowerShell/default.aspx">PowerShell</category></item></channel></rss>