Q:

how to compress log files older than 30 days in windows?

I write below powershell script to compress logs older than 30 days:-
$LastWrite=(get-date).AddDays(-30).ToString("MM/dd/yyyy")

Get-ChildItem -Filter "server.log*" -Recurse -File | Where-Object 
{$_.LastWriteTime -le $LastWrite} 

Now, I am unable to get compressed command in powershell via which I can compress(zip/tar) the server.log* files older than 30 days. 
Expecting a single command which i can use by adding a pipe sign in the above command.

A:

You can use the Compress-Archive cmdlet to zip files if you have PowerShell version 5 or above:
$LastWrite = (get-date).AddDays(-30)

$Files = Get-ChildItem -Filter "server.log*" -Recurse -File | Where-Object {$_.LastWriteTime -le $LastWrite}

ForEach ($File in $Files) {
    $File | Compress-Archive -DestinationPath "$($File.fullname).zip"
}

