Create zip from single file in PSv4

This topic contains 6 replies, has 4 voices, and was last updated by  js 3 months, 3 weeks ago.

  • Author
  • #97274

    Nathan W

    Hey all,
    I am looking for a script to create a zip from a single file on PSv4

    I have been looking and not found any that make since. I have a script that created a extract from a database that is over 300mb and I want to automate compressing it and emailing it. I have all the other parts working except the zip.


  • #97277

    Simon B
    • #97316

      Nathan W

      Sadly that approach does not work. created a .zip but with nothing in it 🙁

  • #97293


    I would use the command line version of 7 zip. Support in powershell or .net isn't great.

  • #97373


    It all depends on what version of PoSH you are on.
    Prior to v5 you had to us .Net, v5 and higher has cmdlets for this.

        # Get parameters, examples, full and Online help for a cmdlet or function
        # Get a list of all functions
        Get-Command -CommandType Function | Format-Table -AutoSize
        # Get a list of all commandlets
        Get-Command -CommandType Cmdlet | Format-Table -AutoSize
        # Get a list of all functions for the specified name
        Get-Command -Name '*archive*' -CommandType Function | Format-Table -AutoSize
        CommandType Name             Version Source                      
        ----------- ----             ------- ------                      
        Function    Compress-Archive Microsoft.PowerShell.Archive
        Function    Expand-Archive Microsoft.PowerShell.Archive
        # Get a list of all commandlets for the specified name
        Get-Command -Name '*archive**'  -CommandType Cmdlet | Format-Table -AutoSize
        # get function / cmdlet details
        (Get-Command -Name Compress-Archive).Parameters
        Get-help -Name Compress-Archive -Examples
        Get-help -Name Compress-Archive -Full
        Get-help -Name Compress-Archive -Online
        Get-Help about_*
        Get-Help about_Functions
        # Find all cmdlets / functions with a target parameter
        Get-Help * -Parameter Append
        Get-Command -CommandType cmdlet `
        | Where-Object { $_.parameters.keys -match 'credential'} `
        | Format-Wide name -AutoSize 
        # All Help topics locations
        explorer "$pshome\$($Host.CurrentCulture.Name)"

    Other pre-built stuff to use...

    Use PowerShell to Create ZIP Archive of Folder

    7Zip4PowerShell 1.8.0
    Powershell module for creating and extracting 7-Zip archives

  • #97401

    Simon B

    This worked for me

    $source = "C:\Temp"

    $destination = "C:\backup\"

    If(Test-path $destination) {Remove-item $destination}

    Add-Type -assembly ""

    [io.compression.zipfile]::CreateFromDirectory($Source, $destination)

  • #97445


    Compress-archive in PS5 doesn't preserve the path in this case:

    Get-ChildItem -recurse .\foo\ | Compress-Archive -DestinationPath

    Oh, it does except for the top level folder.

    EDIT: You can zip a lot of duplicate files this way, since every folder will automatically be zipped recursively.

You must be logged in to reply to this topic.