Powershell : 7zip Compress folders (with password)


In another scripting requirement I had the task to take a folder structure on Windows and then create a zip file based on the name of the folders, this was something done frequently so lets get scripting to make this easier, so this is what the folder looks like, as you can see we have RXTOxxxxxx as the format this will be important in the send script.

The first mission is to create a zip file with the name of the folder as the zip file name, so this is where the script below comes in:

Script : Creating the zip file as the name of the folder, no password

$sourceDir = <path_with_folders>" # Path to folders that require zipping
$7zExe = "C:\Program Files\7-Zip\7z.exe"  # Path to 7z.exe executable

# Get a list of directories within the source directory

$directories = Get-ChildItem -Path $sourceDir -Directory

foreach ($dir in $directories) {
    $zipFileName = "$($dir.Name).zip"
    $zipFilePath = Join-Path -Path $sourceDir -ChildPath $zipFileName
    & $7zExe a -tzip $zipFilePath "$($dir.FullName)\*"
}


That will output this, bingo, exactly what we need, as you can see below the name of the folder in a zip file:


Now the reason for the naming convention at the start is the name we need to password protect the zip files with, so we need to amend our script to read the first 11 characters of the folder name and make that the password, so lets get cracking on that.....

Script : Creating the zip file as the name of the folder, and using the x letters of the folder name as the password

Note : Remember Windows will start the count at "0" not "1" so if you have 11 characters you will need specify 10 characters, so you will get a whitespace or the next logical digit.

$sourceDir = <path_with_folders>" # Path to folders that require zipping
$7zExe = "C:\Program Files\7-Zip\7z.exe"  # Path to 7z.exe executable


# Get a list of directories within the source directory
$directories = Get-ChildItem -Path $sourceDir -Directory

foreach ($dir in $directories) {
    $password = $dir.Name.Substring(0, [Math]::Min(11, $dir.Name.Length))
    $zipFileName = "$($dir.Name).zip"
    $zipFilePath = Join-Path -Path $sourceDir -ChildPath $zipFileName
   & $7zExe a -tzip "-p$password" $zipFilePath "$($dir.FullName)\*"
}

Right so when that runs it looks the same as before, however if you open it in 7zip you will notice that the encrypted marker has a"+" added to it which means it is encrypted:


Now we need to test that the password is correct, so first lets try a bad password as you can see below and what we are expecting is "bad password" not a extraction:


This is good, so the wrong password gets no dice and fails with "wrong password" exactly the goal, as you can see below:


Now lets use the correct password and this should extract the file we have selected:


That is excellent news, that now extracts the file without an error, this means the password in the script is the correct password as predicted:


All mail megaton, another mission complete for now!
Previous Post Next Post

نموذج الاتصال