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

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Okay, let's go

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

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay