title: Powershell script to logrotate logs
body: <p>Ok.So I want to write a PowerShell script that simulates(sort of) the LogRotate option found in Linux.For one single file:no problem.But I want it to work for all the file INSIDE a folder.But...It doesn't work...at all.I tried doing it with foreach-object but it still doesn't work.I'm new to PowerShell programming,so you'll have to excuse me if my script doesn't look so clean.
  Here it is:</p>
  
  <pre><code>function RotateLog {
      $log = "C:\Users\nvali\Desktop\scripts"
      Push-Location $log
      $target = Get-ChildItem $log -Filter "*.txt"
      $threshold = 300
      $datetime = Get-Date -uformat "%Y-%m-%d-%H%M"
      #$target_path =Get-ChildItem $log
      $filesize = $target.length/1MB
      $target | ForEach-Object {
          if ($filesize -ge $threshold) { 
              $filename = $_.name -replace $_.extension,"" 
              $newname = "${filename}_${datetime}.log_old"
              Rename-Item $_.fullname $newname
              $rotationmessage = ""
              Write-Host "Done" 
          }
          echo "$rotationmessage"
      }
  }
  </code></pre>