PreReq : 7zip installation Radiata stories iso.
Below script takes the input as a path of a folder (or a UNC path of a folder) and zip up all the folders which are inside the specified location. Sometime I need to zip up 10 folders and I don’t want to manually go to each folder and zip them up.
For UNC path, I create a PS drive so that the zip can be stored in the same location as that of the folders. Else 7zip stores the files in the current PS directory.
On the rare occasion you might have the need to create a self-extracting executable file. This is an executable file that when double clicked automatically extracts the files contained within. The downside to these files are they aren't.zip they are.exe files.
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 | Param([Parameter(Mandatory=$true,ValueFromPipeLine=$true)][String]$DropSiteFolder) Set-aliassz'$env:ProgramFiles7-Zip7z.exe' if(-not(test-path'$env:ProgramFiles7-Zip7z.exe')){#Check if 7zip exist in the machine } if(test-path$DropSiteFolder){#Check if the folder location/name is valid new-psdrive-namedrop-PSProviderFileSystem-root$DropSiteFolder#Create a new PSDrive for the drop site location else{ throw('Can't create a PSDrive for the location $DropSiteFolder specified.') set-locationdrop:#Change current directory to the drop location Write-Host'Compressing using 7zip' #dir Where-Object { $_.PSIsContainer } ForEach-Object { C:'Program Files'7-zip7z.exe a -mx9 '$_.zip' $_.FullName } #http://www.solo-technology.com/blog/2007/12/14/7-zip-compression-vs-speed/ dirWhere-Object{$_.PSIsContainer}ForEach-Object{sza-mx9-mmt='on''$_.zip'$_.FullName} catch{ } catch{ } Write-Host-ForegroundColorgreen'Folders successfully compressed' |
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 | Param([Parameter(Mandatory=$true,ValueFromPipeLine=$true)][String]$DropSiteFolder) Set-aliassz'$env:ProgramFiles7-Zip7z.exe' if(-not(test-path'$env:ProgramFiles7-Zip7z.exe')){#Check if 7zip exist in the machine } if(test-path$DropSiteFolder){#Check if the folder location/name is valid new-psdrive-namedrop-PSProviderFileSystem-root$DropSiteFolder#Create a new PSDrive for the drop site location else{ throw('Can't create a PSDrive for the location $DropSiteFolder specified.') set-locationdrop:#Change current directory to the drop location Write-Host'Compressing using 7zip' #dir Where-Object { $_.PSIsContainer } ForEach-Object { C:'Program Files'7-zip7z.exe a -mx9 '$_.zip' $_.FullName } #http://www.solo-technology.com/blog/2007/12/14/7-zip-compression-vs-speed/ dirWhere-Object{$_.PSIsContainer}ForEach-Object{sza-mx9-mmt='on''$_.zip'$_.FullName} catch{ } catch{ } Write-Host-ForegroundColorgreen'Folders successfully compressed' |
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 | Param([Parameter(Mandatory=$true,ValueFromPipeLine=$true)][String]$DropSiteFolder) Set-aliassz'$env:ProgramFiles7-Zip7z.exe' if(-not(test-path'$env:ProgramFiles7-Zip7z.exe')){#Check if 7zip exist in the machine } if(test-path$DropSiteFolder){#Check if the folder location/name is valid new-psdrive-namedrop-PSProviderFileSystem-root$DropSiteFolder#Create a new PSDrive for the drop site location else{ throw('Can't create a PSDrive for the location $DropSiteFolder specified.') set-locationdrop:#Change current directory to the drop location Write-Host'Compressing using 7zip' #dir Where-Object { $_.PSIsContainer } ForEach-Object { C:'Program Files'7-zip7z.exe a -mx9 '$_.zip' $_.FullName } #http://www.solo-technology.com/blog/2007/12/14/7-zip-compression-vs-speed/ dirWhere-Object{$_.PSIsContainer}ForEach-Object{sza-mx9-mmt='on''$_.zip'$_.FullName} catch{ } catch{ } Write-Host-ForegroundColorgreen'Folders successfully compressed' |