Thursday, December 4, 2014

Reversing a file with Powershell

Today I was tracking down the source of a deadlock and needed to reverse the SQL Server log file so that it would be easier to follow the deadlock information. A quick search for reversing a text file lead to a command called Tail which must be a *unix command. I also found a couple of Powershell scripts but nothing really simple.

As it happens, get-content returns it's results as an array.
And you can reverse an array using the static Reverse() method
And you can write an array using out-file.

A quick example:
$c = (get-content deadlock.log)
$c | out-file "rev-deadlock.log"