Windows Tip Sheet

What Sort of Admin Are You?

You'll know if you've been doing your job keeping systems in check with this nifty command prompt trick.

The Windows CMD prompt is far from obsolete, especially for those of you where PowerShell simply isn't an option. One often overlooked command is SORT. Back in the golden age of command prompt administration, admins used the Sort command to manipulate text files and data sets. Run Sort /? at a command prompt to see usage and syntax. Here are some examples of how you might use it today.

Open a command prompt and type:

type %windir%\windowsupdate.log | sort /r |more

You'll see your Windows Update log in reverse order. An even better approach might be to filter for Warnings:

find /i "Warning" %windir%\WindowsUpdate.log | sort /r |more

Run this command with PSExec against your clients to troubleshoot Windows Update problems.

Here's another idea: I have a CSV file, localadminage.csv, that contains computername, the name of the administrator account, the password age in days and when it was last changed. The file might look like this:

APP01,Administrator,"610.12",10/21/2005 4:25:05 PM

APP02,Administrator,"573.08",4/13/2005 10:24:25 AM

DC01,Administrator,"287.04",10/24/2006 8:18:44 AM

DC03,Administrator,"287.04",10/23/2006 3:51:25 PM

SQL05,Administrator,"288.79",3/13/2006 1:52:45 PM

CRM01,Administrator,"288.12",3/13/2006 1:52:41 PM

MAIL01,Administrator,"656.11",1/6/2005 2:49:34 PM

ISA01,Administrator,"250.30",2/16/2006 1:34:33 PM

MAIL02,Administrator,"253.20",5/1/2006 6:56:06 PM

I can use the Sort command to sort the file in memory. If I simply type:

Sort localadminage.csv

I'll see a sorted version of the file based on the beginning of each line, which in this case will be the computer name. The original file remains untouched. You can tell the Sort command to create a new file and overwrite the original.

I recommend sending output to a new file:

Sort localadminage.csv /o localadminage-sorted.csv

If I want to sort on a different column, I need a slightly more complex command expression:

(for /f "tokens=1-5 delims=," %a in (localadminage.csv) DO @echo %c,%a) | sort /r

This should be typed as one line. When executed, the FOR command parses the file returning the third (password age) and first (servername) columns.

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at editor@mcpmag.com; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

The output is then sent to the SORT command which does a reverse sort. Because I've specified the password age as the first part of the line, I'll get a list of password ages and computer names with the oldest at the top. Sure you could do this in Microsoft Excel, but this is more fun and faster.

The next time you're looking to sort some data, the solution may be closer than you think.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.