Powershell:压缩文件

Powershell:压缩文件,powershell,powershell-2.0,windows-server-2008-r2,powershell-3.0,powershell-1.0,Powershell,Powershell 2.0,Windows Server 2008 R2,Powershell 3.0,Powershell 1.0,我需要根据文件编写的月份和年份压缩文件 # group files based on extension month/year $groups = Get-ChildItem C:\Users\mypath -filter "*psv.*" | Group-Object {"{0:MMMM} {0:yyyy}" -f $_.LastWriteTime } # compress groups ForEach ($group in $groups) { & "C:\Program

我需要根据文件编写的月份和年份压缩文件

# group files based on extension month/year
$groups = Get-ChildItem C:\Users\mypath -filter "*psv.*" | Group-Object {"{0:MMMM} {0:yyyy}" -f $_.LastWriteTime }

# compress groups
ForEach ($group in $groups) {
    & "C:\Program Files\7-Zip\7z.exe" u ($group.FullName + ".7z") $group.FullName
}
但是,当我单独运行下面这行时,上面的脚本不起作用

Get-ChildItem C:\Users\mypath -filter "*psv.*" | Group-Object {"{0:MMMM} {0:yyyy}" -f $_.LastWriteTime }
我得到了下面的结果,很好。

但是,它不会压缩文件组。

尝试以下操作:

$groups = Get-ChildItem C:\Users\mypath *psv.* | Group {"{0:MMMM}-{0:yyyy}" -f $_.LastWriteTime}
foreach ($group in $groups) { 
    foreach ($file in $group.Group.FullName) { 
         Write-Host "Processing " + $group.Name + " " + $file 
         & "C:\Program Files\7-Zip\7z.exe" u ($group.Name + ".7z") $file
    } 
}
GroupInfo输出的GroupInfo对象的Group属性包含该分组中的所有文件。你必须列举那些文件