有没有最简单的方法来使用PowerShell解压缩文件夹和子文件夹

时间:2014-09-16 16:24:21

标签: powershell

嗨我想解压缩文件夹和子文件夹我尝试了不同的方法和脚本,但没有运气。 有人可以帮忙吗

$Date = Get-Date
#
$folder_date = $Date.ToString("yyyy-MM-dd_HHmmss")


$content = get-childitem 'C:\Deployments'      



$sortedContent = $content |  Sort-Object LastWriteTime -Descending   

# 1. LIST BACKUPS
write-host "This is the list of all the Deployments  :"

$count = 1
foreach ($item in $sortedContent)
{
#Edit: Now with auto-incrementing counter
Write-Host ("{0}: {1}" -f $count, $item.Name)
$count++ 
}

$itemNumber = Read-Host "Please select which deployments you want to copy "
$itemNumber = $itemNumber -1
if($sortedContent[$itemNumber].PSIsContainer -eq $true)
{ 
    $src = $sortedContent[$itemNumber].FullName + "\"
    $Date = Get-Date
    $folder_date = $Date.ToString("yyyy-MM-dd_HHmm")
    $tempPath = 'C:\_Temp\Restore' + $folder_date
    if (!(Test-Path -path $tempPath)) 
    {
        New-Item $tempPath -type directory

    }   

    $TabletzipPath = $src + "table.zip"
    $AdminzipPath = $src + "admin.zip"




    .\unzip.ps1 $src $tempPath
    .\unzip.ps1 $TabletzipPath $tempPath
    .\unzip.ps1 $AdminzipPath $tempPath


}

3 个答案:

答案 0 :(得分:0)

$Shell = new-object -com shell.application
$Zip = $Shell.NameSpace($PathToZip)
ForEach($Item in $Zip.items())
{
    $Shell.Namespace($ExtractPath).copyhere($Item)
}

答案 1 :(得分:0)

如果你只想找一个简单的&快速做到这一点。

您可以使用此功能:

Unzip-File -File C:\ location.zip -Destination E:\ destination

需要此脚本:

http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Function-to-727d6200

答案 2 :(得分:0)

使用 jar 解压:将其复制粘贴到 PowerShell ISE 或使用以下代码创建一个 .ps1 文件:

#choose directory where your script is
cd $PSScriptRoot
#do for each file in your directory, -recurse stands for all subfolders, *.jar stands for all files with jar extension
foreach ($file in (dir -Recurse *.jar)) 
{
#Uncomment this to check files this script is going to unzip (don't forget to comment 7z line below to avoid operation)
#Write-Host ("Dir for file " + $file.Name + " is " + $file.Directory)
 
#put here full path to 7z if there is no system env (or just make it) for that
#x stands for eXtract files with full paths
#-o is for output folder (example of usage -oc:\test\)
7z x $file.FullName ("-o"+$file.Directory)
}

它应该将您需要的所有内容直接解压缩到您的 .jar(或任何其他存档)所在的文件夹中。