System.IO.Compression.ZipFile provides PowerShell access to ZIP archives through .NET. Load System.IO.Compression.FileSystem with Add-Type, then call the class directly using bracketed type names and static methods. section 2.1.6
Use OpenRead to inspect archive entries, then compare each expected file by name and size. Missing files generate nonterminating errors with Write-Error, allowing the remaining checks to continue. section 2.1.6
Listing 2.2 Deleting archived files
Function Remove-ArchivedFiles {
[CmdletBinding()]
[OutputType()]
param(
[Parameter(Mandatory = $true)]
[string]$ZipFile,
[Parameter(Mandatory = $true)]
[object]$FilesToDelete,
[Parameter(Mandatory = $false)]
[switch]$WhatIf = $false
)
$AssemblyName = 'System.IO.Compression.FileSystem' #1
Add-Type -AssemblyName $AssemblyName | Out-Null
$OpenZip = [System.IO.Compression.ZipFile]::OpenRead($ZipFile)
$ZipFileEntries = $OpenZip.Entries #2
foreach($file in $FilesToDelete){ #3
$check = $ZipFileEntries | Where-Object{ $_.Name -eq $file.Name -and
$_.Length -eq $file.Length }
if($null -ne $check){ #4
$file | Remove-Item -Force -WhatIf:$WhatIf #5
}
else {
Write-Error "'$($file.Name)' was not find in '$($ZipFile)'"
}
}
}
A reusable Remove-ArchivedFiles function demonstrates assembly loading, read-only archive access, entry matching, and optional WhatIf-protected deletion. section 2.1.6
