Uli*_*kel 213 powershell
我有一个.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)
}
Run Code Online (Sandbox Code Playgroud)
怎么了?该目录C:\a仍为空.
Kei*_*ill 470
在PowerShell v5 +中,内置了一个Expand-Archive命令(以及Compress-Archive):
Expand-Archive c:\a.zip -DestinationPath c:\a
Run Code Online (Sandbox Code Playgroud)
Mic*_*lli 232
以下是使用System.IO.Compression.ZipFile中的ExtractToDirectory的简单方法:
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Unzip "C:\a.zip" "C:\a"
Run Code Online (Sandbox Code Playgroud)
请注意,如果目标文件夹不存在,ExtractToDirectory将创建它.
NIK*_*NIK 24
在PowerShell v5.1中,与v5相比略有不同.根据MS文档,它必须有一个-Path参数来指定存档文件路径.
Expand-Archive -Path Draft.Zip -DestinationPath C:\Reference
Run Code Online (Sandbox Code Playgroud)
或者,这可能是一个实际的路径:
Expand-Archive -Path c:\Download\Draft.Zip -DestinationPath C:\Reference
Run Code Online (Sandbox Code Playgroud)
Kol*_*yon 13
使用内置的powershell方法Expand-Archive
例子
Expand-Archive -LiteralPath C:\archive.zip -DestinationPath C:\
Run Code Online (Sandbox Code Playgroud)
小智 12
嘿它为我工作..
$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)
}
Run Code Online (Sandbox Code Playgroud)
将Expand-Archivecmdlet与参数集之一一起使用:
Expand-Archive -LiteralPath C:\source\file.Zip -DestinationPath C:\destination
Run Code Online (Sandbox Code Playgroud)
Expand-Archive -Path file.Zip -DestinationPath C:\destination
Run Code Online (Sandbox Code Playgroud)
使用expand-archive但自动创建以存档命名的目录:
function unzip ($file) {
$dirname = (Get-Item $file).Basename
New-Item -Force -ItemType directory -Path $dirname
expand-archive $file -OutputPath $dirname -ShowProgress
}
Run Code Online (Sandbox Code Playgroud)
小智 5
对于那些想要使用Shell.Application.Namespace.Folder.CopyHere()并希望在复制时隐藏进度条或使用更多选项的人,文档位于: https:
//learn.microsoft.com/en-us /windows/desktop/shell/文件夹-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)
Run Code Online (Sandbox Code Playgroud)
Windows Core 版本上使用 Shell.Application 的限制:
https://learn.microsoft.com/en-us/windows-server/administration/server-core/what-is-server-core
在 Windows核心版本上,默认情况下未安装Microsoft-Windows-Server-Shell-Package,因此 shell.applicaton 将不起作用。
注意:以这种方式提取档案将花费很长时间,并且会减慢 Windows GUI 的速度
| 归档时间: |
|
| 查看次数: |
223720 次 |
| 最近记录: |