Catch if ZIP errors


  • I am pretty new to powershell scripting and I came across a potential problem in my future.  I have created a script that checks to see if a file is older that 3 months old and, if so, zips it and deletes the original folder.  The script works perfectly, but I am afraid that if the zip fails, it will still delete the original folder.  I was wondering if there is a way to put a catch in there so that if the zip fails, then it wouldn't remove the folder and it would send an email.  Here is what I have so far.  Thanks.

    ForEach ($i in $dirs)
    if ($i.PSIsContainer)
    $folderdate = $
    if ($ -lt $zipdate)
    Copy-Item $i "zip$zipname" -Recurse
    rm $i -Recurse
    write zip directory
    Write-Zip -level 9 -path "zip$zipname" -OutputPath "$" -IncludeEmptyDirectories

    Monday, November 26, 2012 4:30 PM


All replies

  • What is "write-zip" ?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Monday, November 26, 2012 4:37 PM
  • It is part of the Powershell Community Extension.  It gives the ability to zip a folder in powershell.
    Monday, November 26, 2012 4:44 PM

  • PS C:\> help about_CommonParameters
    PS C:\> help about_try_catch_finally


    Monday, November 26, 2012 4:49 PM