I found this one - > https://superuser.com/questions/1150631/powershell-script-to-test-zip-passwords-from-file - > but it looks like what I need is a little different.
Is it possible to find all files in a folder which are NOT protected by a password and delete them?
Trying to solve the problem of people putting all kinds of sensitive files where these files shouldn't be.
You can try the following:
Get-ChildItem -Filter *.zip |
Where-Object { '' | 7z t $_.FullName *>$null; $LASTEXITCODE -eq 0 } |
Remove-Item -WhatIf
Note: The -WhatIf
common parameter in the command above previews the operation. Remove -WhatIf
once you're sure the operation will do what you want.
The above uses 7z
's t
command to verify archive integrity.
Password-protected archives trigger a password prompt, which the pipeline input - the empty string ('' | ...
) - provides a response to, causing the integrity verification to fail due to an invalid password, which 7z
reports via a nonzero exit code, reflected in PowerShell's automatic $LASTEXITCODE
variable.
$LASTEXITCODE -eq 0
therefore returns $true
only for archives that pass the integrity test, which are archives without password protection (7z
then ignores the unneeded pipeline input).