Let PowerShell to summarize your IM history

(March 25, 2007) Update: Thanks to James Manning for the -passthru parameter to simply the command.

Windows PowerShell, also known as “Monad”, is a cool shell environment to replace old CMD shell under Windows. It is an object oriented shell and it allow easy manipulation of different types of objects interactively. Another advantage is the support of various data structures and formats, such as XML. Just like other good old shells, it also supports piping. The difference is that, instead of text stream, it sends objects through the pipes.

I always want to find out some information about my Live Messenger history. Now let’s see what PowerShell can do with it. In most cases, Live Messenger history is saved under “My Received Files\<Live ID with some numbers>\History” in your Document folder. Now let’s boot into PowerShell and “Set-Location” or ”cd” to this folder. If you type “Get-ChildItem” or “dir” you will see a bunch of .xml files listed.

Alright, let’s first try to find the top 10 friends with most letters:

Get-ChildItem | Sort-Object Length -descending | Select-Object -first 10

At this point, I assume you are already familiar with the concept of piping. Here it gets a list of files, sort it and display the first 10. That’s not too difficult to understand.

Actually those files are in XML format. That’s why it ends with .xml extension. One important XML element in these files is the message. Each message element corresponds to a sentence sent between me and my friend. Let’s see how PowerShell deals with XML files in this case. The following command lists the top 10 friends with most messages:

Get-ChildItem |
ForEach-Object {
    $_ |
    Add-Member noteproperty -name count -value ([XML] (Get-Content $_ -Encoding UTF8)).Log.Message.Count -passthru
} |
Sort-Object -property count -descending |
Select-Object -first 10 |
Format-Table count,Name

It first gets a list of files. For each file, it creates a new object $o. Then it adds the count of messages from the XML file as a property of $o, as well as the name of each file. Finally, it sorts the object based on number of messages and displays the top 10.

Another interesting XML element of Live Messenger history file is LastSessionID. I guess It is the number of chat sessions established by either me or my friend. With a little modification to the previous command, I can know the top 10 friends with most sessions:

Get-ChildItem |
ForEach-Object {
    $_ |
    Add-Member noteproperty -name count -value ([Int32] ([XML] (Get-Content $_ -Encoding UTF8)).Log.LastSessionID) -passthru
} |
Sort-Object -property count -descending |
Select-Object -first 10 |
Format-Table count,Name

A small change is that LastSessionsID is read as a string. So we have to convert it to a integer number using [Int32].

Other XML elements, such as Date, Time and even text in each message, are very informative too, as long as you can explorer them in a good way. Right now, there are other more information I can think of out of my head, e.g. the top 10 longest message you received, the first 10 person talked with you, the longest 10 chat sessions in your IM history and so on.

For updated information and tricks on PowerShell, here is a good resource. If you have used Live Messenger on two or more computers, you might also want to consider this history merger.

Guess who sends the most smiles to me? 🙂

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

%d 博主赞过: