Monday, March 24, 2008

PowerShell - Sorting Text

Over time I will be posting little uses I find for the Windows PowerShell. The PowerShell can be downloaded from Microsoft's website. I downloaded it the first time to try and solve some VBScript woes that I was having. Later, I found that I needed it to manage Exchange 2007 during my training at Global Knowledge (love this place!). Finally, I downloaded an awesome tutorial from Frank Koch. This tutorial is a must for anyone new to the PowerShell. I have been through the Microsoft documentation and user guides, and they are nice but, Frank has put it together in such a way that he steps through the concepts and relates them so that they are much more intuitive and easy to remember.

We have many servers (about 65) in our enterprise that I use Microsoft's Remote Desktops Console to access. As new servers are added and old ones are removed, the list quickly becomes difficult to navigate because the servers are soon in no order at all. The console doesn't allow for the moving up/down in the list or any other easy way to change them. It is possible to go through and rename each connection with what would be the next server in order, but this too is tedious and difficult. Finally, some sort of XML configuration file - but no, I couldn't find that either. I have found it much easier to just add the connections back in.

The PowerShell comes into play here. In the MMC it is possible to export the list of servers to a text file. Once exported, I use the Get-Content cmdlet to read the text file in. I then pipe the results into the sort-object cmdlet and it spits out a beautifully sorted list by connection name. Here is the command that I use to accomplish this:

get-content 'Remote Desktops.txt' | sort-object

Now I can just go down the list adding the connections back to the console and I have a nicely sorted list of servers.

No comments: