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.....
$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)\*"
}
This is good, so the wrong password gets no dice and fails with "wrong password" exactly the goal, as you can see below: