我有一个.zip文件,需要使用Powershell解压缩其全部内容。我在这么做,但似乎并不奏效:
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("C:\a.zip")
MkDir("C:\a")
foreach ($item in $zip.items()) {
$shell.Namespace("C:\a").CopyHere($item)
}
怎么了?目录C:\a仍然为空。
我有一个.zip文件,需要使用Powershell解压缩其全部内容。我在这么做,但似乎并不奏效:
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("C:\a.zip")
MkDir("C:\a")
foreach ($item in $zip.items()) {
$shell.Namespace("C:\a").CopyHere($item)
}
怎么了?目录C:\a仍然为空。
当前回答
嘿,它为我工作..
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("put ur zip file path here")
foreach ($item in $zip.items()) {
$shell.Namespace("destination where files need to unzip").CopyHere($item)
}
其他回答
使用expand-archive,但自动创建以存档命名的目录:
function unzip ($file) {
$dirname = (Get-Item $file).Basename
New-Item -Force -ItemType directory -Path $dirname
expand-archive $file -OutputPath $dirname -ShowProgress
}
使用扩展-归档cmdlet的参数设置之一:
Expand-Archive -LiteralPath C:\source\file.Zip -DestinationPath C:\destination
Expand-Archive -Path file.Zip -DestinationPath C:\destination
嘿,它为我工作..
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("put ur zip file path here")
foreach ($item in $zip.items()) {
$shell.Namespace("destination where files need to unzip").CopyHere($item)
}
对于那些想要使用Shell.Application.Namespace.Folder.CopyHere()并且想要在复制时隐藏进度条,或者使用更多选项的人,文档在这里: https://learn.microsoft.com/en-us/windows/desktop/shell/folder-copyhere
要使用powershell和隐藏进度条并禁用确认,您可以使用如下代码:
# We should create folder before using it for shell operations as it is required
New-Item -ItemType directory -Path "C:\destinationDir" -Force
$shell = New-Object -ComObject Shell.Application
$zip = $shell.Namespace("C:\archive.zip")
$items = $zip.items()
$shell.Namespace("C:\destinationDir").CopyHere($items, 1556)
Shell的使用限制。应用程序在windows核心版本: https://learn.microsoft.com/en-us/windows-server/administration/server-core/what-is-server-core
在windows核心版本中,默认情况下不会安装Microsoft-Windows-Server-Shell-Package,因此shell。应用程序将无法工作。
注意:以这种方式提取档案会花费很长时间,并且会降低windows gui的速度
在PowerShell v5+中,有一个内置的Expand-Archive命令(以及Compress-Archive):
Expand-Archive C:\a.zip -DestinationPath C:\a