How can I create a PowerShell script to get all files of type .BAK and add them to a ZIP file (can I have my PowerShell script take a parameter when called to control the name o
If you're using PowerShell v5 then you can use the Compress-Archive
function instead of using 7zip:
$filePath = "C:\SQLBackups"
$ZipName = "v4.14.4_backups.zip"
$BakFiles = Get-ChildItem -Recurse -Path $filePath -Include *.bak
$BakFiles | Compress-Archive -DestinationPath "$filePath\$ZipName"
$BakFiles | Remove-Item -Force
You name the archive after each file you're processing in the loop, hence each file goes into its own archive. If you want to put all files into the same archive, define the archive name once outside the loop:
$zipfile = Join-Path $filePath 'v4.14.4_backups.zip'
foreach ($file in $bak) {
sz a -t7z $zipfile $file.FullName
}