Вы можете использовать Shell.Application
.
# Create an empty zip file
$byte = @([byte]80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
[System.IO.File]::WriteAllBytes(".\Desktop\zip.zip", $byte)
# New Shell.Application ComObject
$sa = New-Object -ComObject Shell.Application
# Path to folder containing items you wish to zip
$in = $sa.NameSpace("C:\Users\Ash\Desktop\test") # Specify full path
# Path to zip file created earlier
$out = $sa.NameSpace("C:\Users\Ash\Desktop\zip.zip") # Specify full path
# Copy files in to archive.
$out.CopyHere($in.Items(), 4) # 4 = No Progress Box
Folder.CopyHere
Всем нравится многоразовая функция и возможность использовать относительные пути ...
function ConvertTo-Archive {
Param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[Alias("FullName")]
[ValidateScript({Test-Path $_})]
[string]$Path,
[parameter(Mandatory=$true)]
[string]$Output
)
# Convert relative path if one has been used.
$Source = [System.IO.Path]::GetFullPath($Path)
$Destination = [System.IO.Path]::GetFullPath($Output)
$byte = @([byte]80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
[System.IO.File]::WriteAllBytes($Destination, $byte)
$sa = New-Object -ComObject Shell.Application
$in = $sa.NameSpace($Source)
$out = $sa.NameSpace($Destination)
$out.CopyHere($in.Items(), 4)
}
Использование
ConvertTo-Archive -Path .\Desktop\test\ -Output .\Desktop\zip.zip
Или
Get-Item .\Desktop\test\ | ConvertTo-Archive -Output .\Desktop\zip.zip