Powershell Compression Scripts
Below are a few Powershell compression scripts that I created to automate compressing various files on Windows Servers:
Compress Files in a Directory
The first script I created simply compresses all the files in a directory into their own zipfile. I decided to have the resultant filename include the original extension so I knew the zipfile only contained a single file and not a directory.
The biggest obsticle I had was that I had to set an alias for the 7-zip program in order for it to work - so yes, 7-zip is required for this to run.
Download compress_files.ps1 here.
$file_query = "*.txt"
$archivetype="zip"
# Alias for 7-zip - needed otherwise you get Parse Errors
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$files=get-childitem . $file_query | where-object {!($_.psiscontainer)}
ForEach ($file in $files) {
$newfile = ($file.fullname + ".$archivetype")
sz a -mx=5 ($file.fullname + ".$archivetype") $file.fullname
Remove-Item $file
}
Note that you can easily adjust the above script to compress folders instead of files, basically just remove the ! from the $files= directive. Then just adjust the $file_query so it selects the directories you want compressed.
Compress IIS logfiles
On some of the Windows IIS servers that I help maintain, the IIS logfiles grow to hundreds of Megabytes each day (some over GBs of data). This causes space issues since there is a policy in place to keep X number of logfiles for auditing purposes.
So, to reduce the space requirements on these servers I adjusted my compression script above so it automatically compresses the files in the Log directory, except for the current days logfile. I also had to incorporate a way to ensure the new zipfile had the same timestamp as the original file since we remove the files based on their timestamps.
Note that a 2GB logfile can be reduced down to a file that is less than 40MB in size.
Download compress_iislogs.ps1 here.
$log_dir = "C:\Windows\system32\logfiles"
$dir_query = "W3SVC*"
$file_query = "*.log"
$archivetype="zip"
$CurrentDate = Get-Date
$FormattedDate = $CurrentDate.ToString("yyMMdd")
$TodaysFile = "ex$FormattedDate.log"
# Alias for 7-zip - needed otherwise you get Parse Errors
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
cd $log_dir
$dirs=get-childitem . $dir_query | where-object {($_.psiscontainer)}
ForEach ($dir in $dirs) {
cd $dir
$files=get-childitem . $file_query | where-object {!($_.psiscontainer)}
#Powershell doesn't use an array if there is 0 or 1 items in the query, have to code around it.
if ($files.count -gt 0) {
ForEach ($file in $files) {
if ($file -match $TodaysFile) {"Skipping Todays IIS Logfile"}
Else
{
sz a -mx=5 ($file.fullname + ".$archivetype") $file.fullname
$newfile = ($file.fullname + ".$archivetype")
$file_modified = $file.LastWriteTime
(dir $newfile).LastWriteTime = $file_modified
Remove-Item $file
}
}
}
cd ..
}
Note that if you run this script on Windows 2008 server you will have to adjust the $log_dir location.





