Compressing transcripts

I’ve been using PowerShell 5.0 transcripts and to save some space (2/3 actually), I’ve written the following code snippet to compress these files.

The compression ratio formula is taken from this Wikipedia page

$PSTranscripts is a string that points the location where my transcripts are stored (the same location set by GPO). It can be for example: C:\Transcripts

Get-ChildItem -Path $PSTranscripts -Directory | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-1) } |
ForEach-Object {
$StartTime = Get-Date
$FSize = '{0:N2}' -f ((Get-ChildItem -Path $_.FullName -Depth 1 -Recurse | Measure -Property Length -Sum).Sum / 1MB)
try {
Compress-Archive -Path $_.FullName -DestinationPath "$($_.FullName).zip" -CompressionLevel Optimal -Force -ErrorAction Stop
Get-Item -Path $($_.FullName) | Remove-Item -Force -Recurse -Confirm:$false -ErrorAction Stop
} catch {
Write-Warning -Message "Failed because $($_.Exception.Message)"
if (Test-Path -Path "$($_.FullName).zip") {
$Zsize = '{0:N2}' -f ((Get-ChildItem -Path "$($_.FullName).zip").Length/1MB)
$Elpased = (New-TimeSpan -Start $StartTime -End (Get-Date)).ToString()
$ratio = '{0:N2}' -f $FSize/$Zsize
Write-Verbose "It took $Elpased to compress $($_.Name) with a ratio of $ratio : $FSize MB to $Zsize MB zip" -Verbose
} else {
Write-Warning -Message "Skipping $($_.FullName).zip)"

Now, we need the equivalent of bzgrep in the Windows world to be able to parse compressed files 😉

3 thoughts on “Compressing transcripts

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.