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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs