MSH Delete Files Older Than: Step-by-Step Command Tutorial

Written by

in

An MSH script is an old term for Monad Shell scripts, which we know today as Microsoft PowerShell. Before Microsoft released PowerShell in 2006, its code name was “Monad,” and the files used the .msh file extension instead of .ps1.

You can use a simple script to find and delete files older than a certain number of days. Example MSH / PowerShell Script

Here is a basic script that deletes files in a specific folder that are older than 30 days: powershell

\(Folder = "C:\MyLogFolder" \)Days = 30 # Find files older than 30 days and delete them Get-ChildItem \(Folder -Recurse -Force | Where-Object { !\).PSIsContainer -and $.LastWriteTime -lt (Get-Date).AddDays(-\(Days) } | Remove-Item -Force </code> Use code with caution. How the Script Works</p> <p>The script joins a few basic commands together to clean up your files safely:</p> <p><strong><code>Get-ChildItem</code></strong>: This looks inside your target folder. The <code>-Recurse</code> tool tells it to check subfolders, and <code>-Force</code> lets it see hidden files.</p> <p><strong><code>!\).PSIsContainer: This rule makes sure the script only looks at actual files, leaving your folders alone.

$.LastWriteTime: This looks at the date the file was last changed or saved.

(Get-Date).AddDays(-$Days): This takes today’s date and counts backward by 30 days to set your cutoff line.

Remove-Item -Force: This permanently deletes every file that matches your criteria. A Safer Way to Test

If you want to see what files will be deleted before you actually erase them, add -WhatIf to the end of the script: powershell Remove-Item -Force -WhatIf Use code with caution.

This tells the system to print a list of files it intends to delete without actually removing anything. PowerShell Basics: How to Delete Files Older Than X Days

Comments

Leave a Reply

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