title: Powershell: Compress files grouped by month
body: <p>I am using the following script to compress files.</p>
  
    
  
  <pre class="lang-powershell prettyprint-override"><code># set folder path
  $dump_path = "\\somepath\"
  
  # set min age of files
  $max_days = "-30"
  
  # get the current date
  $curr_date = Get-Date
  
  # determine how far back we go based on current date
  $zip_date = $curr_date.AddDays($max_days)
  
  # filter files
  $files = Get-ChildItem $dump_path | Where-Object { ($_.LastWriteTime -lt $zip_date) -and ($_.psIsContainer -eq $false) }
  
  ForEach ($file in $files) {
  
      &amp; "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2 ($file.FullName + ".7z") $file.FullName
  }
  </code></pre>
  
  <p>It works fine, however, It compress each individual file, I was wondering if I could compress files based on the month or week. I saw this option <code>group {"{0:MMM}{0:yyyy}"
  -f $_.CreationTime}</code> on the internet but I am not quite sure how to integrate it with the current script.</p>
  
  <p>I executed the following line straight in powershell and it works, but when I try to integrate it with the above script it doesn't work, maybe the loop needs tweaking.</p>
  
  <pre class="lang-powershell prettyprint-override"><code>Get-ChildItem C:\Users\mypath -filter "*psv.*" | group {"{0:MMMM} {0:yyyy}" -f $_.CreationTime} 
  </code></pre>
  
  <p>and it works fine, but when it comes to compressing i Cant get it to work</p>
  
  <p><img src="https://i.stack.imgur.com/pSVtH.png" alt="enter image description here"></p>
  
  <p>ps. I will also be using <code>if ($LASTEXITCODE -eq 0) {Remove-Item $file}</code> to delete original files afterwards</p>