Powershell: compress files using 7-Zip

This post shows you how to compress a set of files (*.bak) in a directory, using a Powershell script and 7-Zip.

Later, delete the original files and leave only the compressed ones.

Save the script with “.ps1” extension and fire it from a Powershell command line, from a “.bat” file or set it as a Windows scheduled task.

$bkdir = "E:\BackupsPWS"
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'
get-childitem -path $bkdir | Sort-Object length |
where { $_.extension -match ".(bak)" -and
-not (test-path ($_.fullname -replace "(bak)", "7z")) } |
foreach {
$zipfilename = ($_.fullname -replace "bak", "7z")
Invoke-Expression "$7Zip a $zipfilename $($_.FullName)"
}
get-childitem -path $bkdir |
where { $_.extension -match ".(bak)" -and
(test-path ($_.fullname -replace "(bak)", "7z")) } |
foreach { del $_.fullname }

References:

Compressing SQL Server Backups With Windows PowerShell and 7-zip

Calling 7-Zip from powershell

One response to this post.

  1. Skip World Cup Beauty Contest has not too long ago held
    on June seventeen, 2011 in Germany which attracts both countrywide and worldwide consideration.
    The pageant takes area as a component of the run up to the 2011 FIFA Women’s World Cup soccer
    event in Germany this summer. Representative from New Zealand,
    Miss out on Dominique Ela Wisniewski wins the title of
    Miss out on World Cup 2011 (Miss WM – WeltMeisterschaft) in the 2011 attractiveness pageant gala at
    Europa Park in the southern German town of Rust. The 2nd area goes to Christina Trost of Germany who is followed by Jane
    Ogbe of Nigeria in the third location.

    Responder

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: