Use

Best data-sd-animate=”

It looks like the title you provided is incomplete or contains an unfinished HTML fragment. I’ll assume you want an article titled “Best MSH Delete Files Older Than” and write a concise, actionable guide about using PowerShell (MSH) to delete files older than a specified age.

Overview

Use PowerShell to find and remove files older than a given number of days. Common uses: free disk space, rotate logs, and clean temp folders. This guide includes safe testing, examples, scheduled tasks, and best practices.

Safety first

  • Test using Get-ChildItem and -WhatIf before deleting.
  • Run as a user with least privilege needed.
  • Backup important data before bulk deletes.

Basic command

To list files older than 30 days:

Get-ChildItem -Path “C:\Path\To\Folder” -File -Recurse | Where-Object { \(_.LastWriteTime -lt (Get-Date).AddDays(-30) }</span></span></code></pre></div></div><p><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">To</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">delete</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">them</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">(dry</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">run</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">with</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">WhatIf):</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl border border-border bg-sidebar p-2" data-language="" data-streamdown="code-block" style="content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="flex h-8 items-center text-muted-foreground text-xs" data-language="" data-streamdown="code-block-header"><span class="ml-1 font-mono lowercase"></span></div><div class="pointer-events-none sticky top-2 z-10 -mt-10 flex h-8 items-center justify-end"><div class="pointer-events-auto flex shrink-0 items-center gap-2 rounded-md border border-sidebar bg-sidebar/80 px-1.5 py-1 supports-[backdrop-filter]:bg-sidebar/70 supports-[backdrop-filter]:backdrop-blur" data-streamdown="code-block-actions"><button class="cursor-pointer p-1 text-muted-foreground transition-all hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50" data-streamdown="code-block-download-button" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M8.375 0C8.72 0 9 .28 9 .625v9.366l2.933-2.933a.625.625 0 0 1 .884.884l-2.94 2.94c-.83.83-2.175.83-3.005 0l-2.939-2.94a.625.625 0 0 1 .884-.884L7.75 9.991V.625C7.75.28 8.03 0 8.375 0m-4.75 13.75a.625.625 0 1 0 0 1.25h9.75a.625.625 0 1 0 0-1.25z"></path></svg></button><button class="cursor-pointer p-1 text-muted-foreground transition-all hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50" data-streamdown="code-block-copy-button" title="Copy Code" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M11.049 5c.648 0 1.267.273 1.705.751l1.64 1.79.035.041c.368.42.571.961.571 1.521v4.585A2.31 2.31 0 0 1 12.688 16H8.311A2.31 2.31 0 0 1 6 13.688V7.312A2.31 2.31 0 0 1 8.313 5zM9.938-.125c.834 0 1.552.496 1.877 1.208a4 4 0 0 1 3.155 3.42c.082.652-.777.968-1.22.484a2.75 2.75 0 0 0-1.806-2.57A2.06 2.06 0 0 1 9.937 4H6.063a2.06 2.06 0 0 1-2.007-1.584A2.75 2.75 0 0 0 2.25 5v7a2.75 2.75 0 0 0 2.66 2.748q.054.17.123.334c.167.392-.09.937-.514.889l-.144-.02A4 4 0 0 1 1 12V5c0-1.93 1.367-3.54 3.185-3.917A2.06 2.06 0 0 1 6.063-.125zM8.312 6.25c-.586 0-1.062.476-1.062 1.063v6.375c0 .586.476 1.062 1.063 1.062h4.374c.587 0 1.063-.476 1.063-1.062V9.25h-1.875a1.125 1.125 0 0 1-1.125-1.125V6.25zM12 8h1.118L12 6.778zM6.063 1.125a.813.813 0 0 0 0 1.625h3.875a.813.813 0 0 0 0-1.625z"></path></svg></button></div></div><div class="overflow-hidden rounded-md border border-border bg-background p-4 text-sm" data-language="" data-streamdown="code-block-body"><pre class="bg-[var(--sdm-bg,inherit] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit)]" style=""><code class="[counter-increment:line_0] [counter-reset:line]"><span class="block before:content-[counter(line)] before:inline-block before:[counter-increment:line] before:w-6 before:mr-4 before:text-[13px] before:text-right before:text-muted-foreground/50 before:font-mono before:select-none"><span class="text-[var(--sdm-c,inherit)] dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]" style="">Get-ChildItem -Path "C:\Path\To\Folder" -File -Recurse | Where-Object { \).LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -WhatIf

To perform the actual delete:

Get-ChildItem -Path “C:\Path\To\Folder” -File -Recurse | Where-Object { $.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -Force

Advanced options

  • Delete by CreationTime instead of LastWriteTime:
    … | Where-Object { \(_.CreationTime -lt (Get-Date).AddDays(-30) }</span></span></code></pre></div></div> </li> <li class="py-1 [&>p]:inline" data-streamdown="list-item"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">Exclude</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">specific</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">extensions:</span> <div class="my-4 flex w-full flex-col gap-2 rounded-xl border border-border bg-sidebar p-2" data-language="" data-streamdown="code-block" style="content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="flex h-8 items-center text-muted-foreground text-xs" data-language="" data-streamdown="code-block-header"><span class="ml-1 font-mono lowercase"></span></div><div class="pointer-events-none sticky top-2 z-10 -mt-10 flex h-8 items-center justify-end"><div class="pointer-events-auto flex shrink-0 items-center gap-2 rounded-md border border-sidebar bg-sidebar/80 px-1.5 py-1 supports-[backdrop-filter]:bg-sidebar/70 supports-[backdrop-filter]:backdrop-blur" data-streamdown="code-block-actions"><button class="cursor-pointer p-1 text-muted-foreground transition-all hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50" data-streamdown="code-block-download-button" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M8.375 0C8.72 0 9 .28 9 .625v9.366l2.933-2.933a.625.625 0 0 1 .884.884l-2.94 2.94c-.83.83-2.175.83-3.005 0l-2.939-2.94a.625.625 0 0 1 .884-.884L7.75 9.991V.625C7.75.28 8.03 0 8.375 0m-4.75 13.75a.625.625 0 1 0 0 1.25h9.75a.625.625 0 1 0 0-1.25z"></path></svg></button><button class="cursor-pointer p-1 text-muted-foreground transition-all hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50" data-streamdown="code-block-copy-button" title="Copy Code" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M11.049 5c.648 0 1.267.273 1.705.751l1.64 1.79.035.041c.368.42.571.961.571 1.521v4.585A2.31 2.31 0 0 1 12.688 16H8.311A2.31 2.31 0 0 1 6 13.688V7.312A2.31 2.31 0 0 1 8.313 5zM9.938-.125c.834 0 1.552.496 1.877 1.208a4 4 0 0 1 3.155 3.42c.082.652-.777.968-1.22.484a2.75 2.75 0 0 0-1.806-2.57A2.06 2.06 0 0 1 9.937 4H6.063a2.06 2.06 0 0 1-2.007-1.584A2.75 2.75 0 0 0 2.25 5v7a2.75 2.75 0 0 0 2.66 2.748q.054.17.123.334c.167.392-.09.937-.514.889l-.144-.02A4 4 0 0 1 1 12V5c0-1.93 1.367-3.54 3.185-3.917A2.06 2.06 0 0 1 6.063-.125zM8.312 6.25c-.586 0-1.062.476-1.062 1.063v6.375c0 .586.476 1.062 1.063 1.062h4.374c.587 0 1.063-.476 1.063-1.062V9.25h-1.875a1.125 1.125 0 0 1-1.125-1.125V6.25zM12 8h1.118L12 6.778zM6.063 1.125a.813.813 0 0 0 0 1.625h3.875a.813.813 0 0 0 0-1.625z"></path></svg></button></div></div><div class="overflow-hidden rounded-md border border-border bg-background p-4 text-sm" data-language="" data-streamdown="code-block-body"><pre class="bg-[var(--sdm-bg,inherit] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit)]" style=""><code class="[counter-increment:line_0] [counter-reset:line]"><span class="block before:content-[counter(line)] before:inline-block before:[counter-increment:line] before:w-6 before:mr-4 before:text-[13px] before:text-right before:text-muted-foreground/50 before:font-mono before:select-none"><span class="text-[var(--sdm-c,inherit)] dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]" style="">... | Where-Object { \).LastWriteTime -lt (Get-Date).AddDays(-30) -and $.Extension -notin ‘.config’,‘.ps1’ }
  • Remove empty directories after deleting files:
    Get-ChildItem -Path “C:\Path\To\Folder” -Directory -Recurse | Where-Object { @(Get-ChildItem \(_.FullName -File -Recurse).Count -eq 0 } | Remove-Item -Force</span></span></code></pre></div></div> </li> </ul><h3 class="mt-6 mb-2 font-semibold text-xl" data-streamdown="heading-3"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">Scheduling</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">cleanup</span></h3><p><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Create</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">a</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">scheduled</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">task</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">to</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">run</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">a</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">PowerShell</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">script</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">weekly:</span></p><ol class="list-inside list-decimal whitespace-normal [li_&]:pl-6" data-streamdown="ordered-list"> <li class="py-1 [&>p]:inline" data-streamdown="list-item"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Save</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">the</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">deletion</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">command</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">in</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">a</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">.ps1</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">file</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">(avoid</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">hardcoding</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">sensitive</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">creds).</span></li> <li class="py-1 [&>p]:inline" data-streamdown="list-item"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Use</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Task</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Scheduler:</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Action</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">=</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Start</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">a</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">program</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">-></span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Program/script:</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">powershell.exe;</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Arguments:</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">-NoProfile</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">-ExecutionPolicy</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Bypass</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">-File</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">"C:\Scripts\Cleanup.ps1"</span></li> </ol><h3 class="mt-6 mb-2 font-semibold text-xl" data-streamdown="heading-3"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">Best</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">practices</span></h3><ul class="list-inside list-disc whitespace-normal [li_&]:pl-6" data-streamdown="unordered-list"> <li class="py-1 [&>p]:inline" data-streamdown="list-item"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Start</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">with</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">longer</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">age</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">thresholds</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">(e.g.,</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">90</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">days)</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">until</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">confident.</span></li> <li class="py-1 [&>p]:inline" data-streamdown="list-item"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">Log</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">deletions</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">to</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">a</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">file</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">for</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">auditing.</span></li> <li class="py-1 [&>p]:inline" data-streamdown="list-item"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">Exclude</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 0ms; --sd-easing: ease-in;">critical</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">folders</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">and</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">file</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">types.</span></li> <li class="py-1 [&>p]:inline" data-streamdown="list-item"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">Use</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">-WhatIf</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">in</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">testing</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">and</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">add</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">-Confirm</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">for</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">interactive</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">safety.</span></li> </ul><h3 class="mt-6 mb-2 font-semibold text-xl" data-streamdown="heading-3"><span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">Example</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">script</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">with</span> <span data-sd-animate="true" style="--sd-animation: sd-fadeIn; --sd-duration: 250ms; --sd-easing: ease-in;">logging</span></h3><div class="my-4 flex w-full flex-col gap-2 rounded-xl border border-border bg-sidebar p-2" data-language="" data-streamdown="code-block" style="content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="flex h-8 items-center text-muted-foreground text-xs" data-language="" data-streamdown="code-block-header"><span class="ml-1 font-mono lowercase"></span></div><div class="pointer-events-none sticky top-2 z-10 -mt-10 flex h-8 items-center justify-end"><div class="pointer-events-auto flex shrink-0 items-center gap-2 rounded-md border border-sidebar bg-sidebar/80 px-1.5 py-1 supports-[backdrop-filter]:bg-sidebar/70 supports-[backdrop-filter]:backdrop-blur" data-streamdown="code-block-actions"><button class="cursor-pointer p-1 text-muted-foreground transition-all hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50" data-streamdown="code-block-download-button" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M8.375 0C8.72 0 9 .28 9 .625v9.366l2.933-2.933a.625.625 0 0 1 .884.884l-2.94 2.94c-.83.83-2.175.83-3.005 0l-2.939-2.94a.625.625 0 0 1 .884-.884L7.75 9.991V.625C7.75.28 8.03 0 8.375 0m-4.75 13.75a.625.625 0 1 0 0 1.25h9.75a.625.625 0 1 0 0-1.25z"></path></svg></button><button class="cursor-pointer p-1 text-muted-foreground transition-all hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50" data-streamdown="code-block-copy-button" title="Copy Code" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M11.049 5c.648 0 1.267.273 1.705.751l1.64 1.79.035.041c.368.42.571.961.571 1.521v4.585A2.31 2.31 0 0 1 12.688 16H8.311A2.31 2.31 0 0 1 6 13.688V7.312A2.31 2.31 0 0 1 8.313 5zM9.938-.125c.834 0 1.552.496 1.877 1.208a4 4 0 0 1 3.155 3.42c.082.652-.777.968-1.22.484a2.75 2.75 0 0 0-1.806-2.57A2.06 2.06 0 0 1 9.937 4H6.063a2.06 2.06 0 0 1-2.007-1.584A2.75 2.75 0 0 0 2.25 5v7a2.75 2.75 0 0 0 2.66 2.748q.054.17.123.334c.167.392-.09.937-.514.889l-.144-.02A4 4 0 0 1 1 12V5c0-1.93 1.367-3.54 3.185-3.917A2.06 2.06 0 0 1 6.063-.125zM8.312 6.25c-.586 0-1.062.476-1.062 1.063v6.375c0 .586.476 1.062 1.063 1.062h4.374c.587 0 1.063-.476 1.063-1.062V9.25h-1.875a1.125 1.125 0 0 1-1.125-1.125V6.25zM12 8h1.118L12 6.778zM6.063 1.125a.813.813 0 0 0 0 1.625h3.875a.813.813 0 0 0 0-1.625z"></path></svg></button></div></div><div class="overflow-hidden rounded-md border border-border bg-background p-4 text-sm" data-language="" data-streamdown="code-block-body"><pre class="bg-[var(--sdm-bg,inherit] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit)]" style="--sdm-bg: transparent; --sdm-fg: inherit;"><code class="[counter-increment:line_0] [counter-reset:line]"><span class="block before:content-[counter(line)] before:inline-block before:[counter-increment:line] before:w-6 before:mr-4 before:text-[13px] before:text-right before:text-muted-foreground/50 before:font-mono before:select-none"><span class="text-[var(--sdm-c,inherit)] dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]" style="">\)path = “C:\Path\To\Folder”\(days = 30</span></span><span class="block before:content-[counter(line)] before:inline-block before:[counter-increment:line] before:w-6 before:mr-4 before:text-[13px] before:text-right before:text-muted-foreground/50 before:font-mono before:select-none"><span class="text-[var(--sdm-c,inherit)] dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]">\)log = “C:\Scripts\cleanup.log”\(files = Get-ChildItem -Path \)path -File -Recurse | Where-Object { \(_.LastWriteTime -lt (Get-Date).AddDays(-\)days) }foreach (\(f in \)files) {try {        Remove-Item -Path \(f.FullName -Force</span></span><span class="block before:content-[counter(line)] before:inline-block before:[counter-increment:line] before:w-6 before:mr-4 before:text-[13px] before:text-right before:text-muted-foreground/50 before:font-mono before:select-none"><span class="text-[var(--sdm-c,inherit)] dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]">        "\)((Get-Date).ToString(’s’)) Deleted: \((\)f.FullName)” | Out-File -FilePath \(log -Append</span></span><span class="block before:content-[counter(line)] before:inline-block before:[counter-increment:line] before:w-6 before:mr-4 before:text-[13px] before:text-right before:text-muted-foreground/50 before:font-mono before:select-none"><span class="text-[var(--sdm-c,inherit)] dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]">    } catch {</span></span><span class="block before:content-[counter(line)] before:inline-block before:[counter-increment:line] before:w-6 before:mr-4 before:text-[13px] before:text-right before:text-muted-foreground/50 before:font-mono before:select-none"><span class="text-[var(--sdm-c,inherit)] dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]">        "\)((Get-Date).ToString(’s’)) Failed: \((\)f.FullName) - \((\)_.Exception.Message)” | Out-File -FilePath $log -Append    }}

    Conclusion

    Use PowerShell carefully to automate removal of files older than a set age. Test with -WhatIf, log actions, and schedule only after validating behavior.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *