DEV Community

redhcp
redhcp

Posted on

2 1

Powershell find size logs and delete

This script is for list or delete logsfiles. You can change parameters to adjust code for You need.

Invoke-Comman for many host

$machines = @(
    'COMPUTER_NAME_1',
    'COMPUTER_NAME_2'
)
foreach ($machine in $machines) {
    Invoke-Command -ComputerName $machine -ScriptBlock {

        Write-Host "Host: $env:COMPUTERNAME ($(Get-Date -Format MM/dd/yyyy)) "  -foregroundcolor "green"

        #LIST SIZE
        $path ='C:\logs'
        $days = -7  #numer days you can preserve in negative - last 7 days
        $t = ((Get-Childitem -Path $path -Recurse -Include *.log  | Where { $_.Length / 1gb -gt 0 } | Measure-Object Length -Sum | Where-Object LastWriteTime -LT (Get-Date).AddDays($days) | select-object -ExpandProperty Sum)/1GB).ToString('0.00')
        Write-host "Total Size: $t GB " -NoNewline -foregroundcolor "yellow"
        Write-host "PATH: $path"


        #DELETE
        $path ='C:\logs'
        $days = -7  #numer days you can preserve in negative - last 7 days
        $t = ((Get-Childitem -Path $path -Recurse -Include *.log  | Where { $_.Length / 1gb -gt 0 } | Measure-Object Length -Sum | Where-Object LastWriteTime -LT (Get-Date).AddDays($days) | select-object -ExpandProperty Sum)/1GB).ToString('0.00') | -Remove-Item -recurse
        Write-host "Total Size: $t GB " -NoNewline -foregroundcolor "yellow"
        Write-host "PATH: $path"

    }
}
Enter fullscreen mode Exit fullscreen mode

LOCAL HOST or Enter-PSSession

Write-Host "Host: $env:COMPUTERNAME ($(Get-Date -Format “MM/dd/yyyy”)) "  -foregroundcolor "green"

        #LIST SIZE
        $path ='C:\logs'
        $days = -7  #numer days you can preserve in negative - last 7 days
        $t = ((Get-Childitem -Path $path -Recurse -Include *.log  | Where { $_.Length / 1gb -gt 0 } | Measure-Object Length -Sum | Where-Object LastWriteTime -LT (Get-Date).AddDays($days) | select-object -ExpandProperty Sum)/1GB).ToString('0.00')
        Write-host "Total Size: $t GB " -NoNewline -foregroundcolor "yellow"
        Write-host "PATH: $path"


        #DELETE
        $path ='C:\logs'
        $days = -7  #numer days you can preserve in negative - last 7 days
        $t = ((Get-Childitem -Path $path -Recurse -Include *.log  | Where { $_.Length / 1gb -gt 0 } | Measure-Object Length -Sum | Where-Object LastWriteTime -LT (Get-Date).AddDays($days) | select-object -ExpandProperty Sum)/1GB).ToString('0.00') | -Remove-Item -recurse
        Write-host "Total Size: $t GB " -NoNewline -foregroundcolor "yellow"
        Write-host "PATH: $path"


Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more