I have a folder with files. I need to filter according to an extension and to ZIP one by one
$files = Get-ChildItem -path "C:\Temp\SharedFolder\SideVIP\*" -Filter *.VIP
foreach ($file in $files) {
Need some help with the rest
In PowerShell 4+, you can use Compress-Archive
$destPath = 'X:\NewPath'
foreach($file in $files) {
Compress-Archive -Path $file -DestinationPath "$destPath\$($file.BaseName).zip"
}
And if you need to "unzip":
$files = Get-ChildItem C:\Temp\SharedFolder\SideVIP\*.zip
foreach($file in $files) { Expand-Archive -Path $file -DestinationPath . -Force }
$file instances stringifying to their full path, that isn't always the case in Windows PowerShell (it now is in PowerShell (Core) 7+), so it is safer to use $file.FullName explicitly.-Path argument. But - in Windows PowerShell - the seemingly equivalent Get-ChildItem C:\Temp\SharedFolder\SideVIP -Filter *.zip would not work. This headache has gone away in PowerShell Core (v6+). See this answer for details.. Yes, it is sad that PowerShell's cmdlets stringify [System.IO.FileInfo] instances instead of using them as-is, as objects.[System.IO.FileInfo] and [System.IO.DirectoryInfo] instances in Windows PowerShell.Creating zip file using Power shell. first need to install 7Zip software
function create-7zip([String] $aDirectory, [String] $aZipfile){
[string]$pathToZipExe = "$($Env:ProgramFiles)\7-Zip\7z.exe";
[Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory", "-r";
& $pathToZipExe $arguments;
}
Remove-item C:\BackupDummy\*.zip
Get-ChildItem C:\Satwadhir\ |
Group-Object -Property { $_.Name.Substring(0, $_.Name.Length) } |
% { create-7zip $_.Group.FullName "C:\BackupDummy\$($_.Name).zip" }