Expand-NinjaOne7Zip.ps1


Description

Purpose

Extracts files from a NinjaOne Zip file using 7-Zip.

Detailed Description

The Expand-NinjaOne7Zip function extracts files from a NinjaOne Zip file using 7-Zip utility. It provides options to specify the destination folder, password (if required), and specific files to extract.

Back to Top

Usage

Example 1

Expand-NinjaOne7Zip -ZipFile "C:\Path\To\NinjaOne.zip" -Destination "C:\ExtractedFiles"

This example extracts all files from the “NinjaOne.zip” file to the “C:\ExtractedFiles” folder.

Example 2

Expand-NinjaOne7Zip -ZipFile "C:\Path\To\NinjaOne.zip" -Destination "C:\ExtractedFiles" -Password "password" -FilesToExtract "file1.txt", "file2.txt"

This example extracts only “file1.txt” and “file2.txt” from the “NinjaOne.zip” file to the “C:\ExtractedFiles” folder, using the specified password.

Back to Top

Notes

No additional notes.

Back to Top


Script

<#
.SYNOPSIS
Extracts files from a NinjaOne Zip file using 7-Zip.

.DESCRIPTION
The Expand-NinjaOne7Zip function extracts files from a NinjaOne Zip file using 7-Zip utility. It provides options to specify the destination folder, password (if required), and specific files to extract.

.PARAMETER ZipFile
Specifies the path to the NinjaOne Zip file that needs to be extracted. This parameter is mandatory.

.PARAMETER Destination
Specifies the path to the destination folder where the extracted files will be placed. This parameter is mandatory.

.PARAMETER Password
Specifies the password for the NinjaOne Zip file, if it is password protected. This parameter is optional.

.PARAMETER FilesToExtract
Specifies an array of specific files to extract from the NinjaOne Zip file. This parameter is optional.

.EXAMPLE
Expand-NinjaOne7Zip -ZipFile "C:\Path\To\NinjaOne.zip" -Destination "C:\ExtractedFiles"

This example extracts all files from the "NinjaOne.zip" file to the "C:\ExtractedFiles" folder.

.EXAMPLE
Expand-NinjaOne7Zip -ZipFile "C:\Path\To\NinjaOne.zip" -Destination "C:\ExtractedFiles" -Password "password" -FilesToExtract "file1.txt", "file2.txt"

This example extracts only "file1.txt" and "file2.txt" from the "NinjaOne.zip" file to the "C:\ExtractedFiles" folder, using the specified password.

#>
function Expand-NinjaOne7Zip {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
        [string]$ZipFile,
        [Parameter(Mandatory = $true)]
        [ValidateScript({ Test-Path -Path $_ -PathType Container })]
        [string]$Destination,
        [Parameter(Mandatory = $false)]
        [string]$Password,
        [Parameter(Mandatory = $false)]
        [string[]]$FilesToExtract
    )

    try {
        # Unzip the NinjaOne Zip file using 7-Zip
        $arguments = "e `"$ZipFile`" -o`"$Destination`" -y"
        if ($Password) {
            $arguments += " -p`"$Password`""
        }
        if ($FilesToExtract) {
            foreach ($file in $FilesToExtract) {
                $arguments += " `"$file`""
            }
        }
        Start-Process -FilePath "7z" -ArgumentList $arguments -NoNewWindow -Wait -ErrorAction Stop
    }
    catch {
        Write-Error "Failed to unzip file '$ZipFile' to destination '$Destination': $_"
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top