darkhangelsk
darkhangelsk

Reputation: 11

Zipping files using Powershell

I have a script below to zip what's in folder.txt. But what I want is to zip including the folders one by one and put on the same location.

$Src = "C:\Users\Asus\Documents\PowerShell\Folder.txt"
Compress-Archive –Path (Get-Content $Src) -DestinationPath C:\Users\Asus\Documents\PowerShell

let's say:

content in folder.txt:

New Folder

New Folder 2

what will happen is i will have New folder.zip and New Folder 2.zip in same location (C:\Users\Asus\Documents\PowerShell)

Can anyone help me on this? i have a lot of folders that need to be zipped but need to be specified.

If also possible to put password (same password for all, like 'abcd') as i am using SecureZip.

Thank you!

Upvotes: 1

Views: 1003

Answers (1)

postanote
postanote

Reputation: 16076

Continuing from my comment. Choose a resource, review and tweak as needed.

'powershell 7ip folders with password'

Example hit(s)

PowerShell function to create a password protected zip file Download the function script

function Write-ZipUsing7Zip
{
    Param
    (
        [string]$FilesToZip, 
        [string]$ZipOutputFilePath, 
        [string]$Password, 
        [ValidateSet('7z','zip','gzip','bzip2','tar','iso','udf')]
        [string]$CompressionType = 'zip', 
        [switch]$HideWindow
    )
    # Look for the 7zip executable.
    $pathTo32Bit7Zip = "C:\Program Files (x86)\7-Zip\7z.exe"
    $pathTo64Bit7Zip = "C:\Program Files\7-Zip\7z.exe"

    $THIS_SCRIPTS_DIRECTORY = Split-Path $script:MyInvocation.MyCommand.Path
    $pathToStandAloneExe = Join-Path $THIS_SCRIPTS_DIRECTORY "7za.exe"

    if (Test-Path $pathTo64Bit7Zip) { $pathTo7ZipExe = $pathTo64Bit7Zip }
    elseif (Test-Path $pathTo32Bit7Zip) { $pathTo7ZipExe = $pathTo32Bit7Zip }
    elseif (Test-Path $pathToStandAloneExe) { $pathTo7ZipExe = $pathToStandAloneExe }
    else { throw "Could not find the 7-zip executable." }

    # Delete the destination zip file if it already exists (i.e. overwrite it).
    if (Test-Path $ZipOutputFilePath) 
    { Remove-Item $ZipOutputFilePath -Force }

    $windowStyle = "Normal"
    if ($HideWindow) { $windowStyle = "Hidden" }

    # Create the arguments to use to zip up the files.
    # Command-line argument syntax can be found at: http://www.dotnetperls.com/7-zip-examples
    $arguments = "a -t$CompressionType ""$ZipOutputFilePath"" ""$FilesToZip"" -mx9"
    if (!([string]::IsNullOrEmpty($Password))) 
    { $arguments += " -p$Password" }

    # Zip up the files.
    $p = Start-Process $pathTo7ZipExe -ArgumentList $arguments -Wait -PassThru -WindowStyle $windowStyle

    # If the files were not zipped successfully.
    if (!(($p.HasExited -eq $true) -and ($p.ExitCode -eq 0)))
    {
        throw "There was a problem creating the zip file '$ZipFilePath'."
    }
}

BTW, there are PowerShell add-on modules in the Microsoft powershellgallery.com for 7Zip.

Find-Module -Name '*zip*' | Format-Table -AutoSize

# Results
<#
Version Name            Repository Description                                                                                                                        
------- ----            ---------- -----------                                                                                                                        
1.13.0  7Zip4Powershell PSGallery  Powershell module for creating and extracting 7-Zip archives                                                                       
0.3.1   Zip             PSGallery  PowerShell module for creating ZIP archives.                                                                                       
2.2.0   PS7Zip          PSGallery  Powershell module that allows you to work with compressed archives                                                                 
1.0.8   x7Zip           PSGallery  Powershell DSC Configuration Script for installing 7-Zip versions 15.07, 15.06, 15.05, 9.38, and 9.20. This Configuration Script...
1.3.4   7ZipArchiveDsc  PSGallery  PowerShell DSC Resource to expand an archive file to a specific path.                                                              
0.2.2   ziphelper       PSGallery  Tools to work with zip archives                                                                                                    
1.1.0   7zip-Archive    PSGallery  7zip utility wrapper....                                                                                                           
0.1.1   Get-GzipContent PSGallery  Gets the content of the gzip archive at the specified location.  
#>

See this article on it.

https://www.sans.org/blog/powershell-7-zip-module-versus-compress-archive-with-encryption

Upvotes: 1

Related Questions