Skip to the main content.

ScriptRunner Blog

Automate your spring cleaning with PowerShell

Table of contents



Post Featured Image

Learn how to automate your digital spring cleaning with PowerShell.

Combining spring cleaning with PowerShell can make the often tedious task of organizing and cleaning your digital environment both efficient and satisfying. Here are three example use cases that help you to perform a comprehensive digital spring cleaning. 

Caution: Always create a backup before you start deleting files!


Clean up old files

The following PowerShell snippet will remove files from C:\Temp directory that are older than 30 days. The "-WhatIf" parameter enables you to simulate the deletion process and check the results first.

$Path = "C:\Temp"
$Days = -30
Get-ChildItem -Path $Path -Recurse | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt (Get-Date).AddDays($Days) } | Remove-Item -Force -WhatIf


Re-organize documents and media

This PowerShell snippet will create new folders for each file type in your Documents folder and move the files to the newly created folders. 

$Path = "$env:USERPROFILE\Documents"
Get-ChildItem -Path $Path -File | ForEach-Object {
$NewFolder = Join-Path -Path $Path -ChildPath $_.Extension.TrimStart('.')
if (-not (Test-Path -Path $NewFolder)) {
New-Item -Path $NewFolder -ItemType Directory
Move-Item -Path $_.FullName -Destination $NewFolder


Delete all files in temp folder

Here's a PowerShell snippet that will delete all files in a specified temporary folder, including sub-folders, but keep the folders itself intact. 

$tempFolderPath = "C:\Path\To\Your\Temp\Folder"

$files = Get-ChildItem -Path $tempFolderPath -File -Recurse
foreach ($file in $files) {
Remove-Item $file.FullName -Force



I hope these three examples give you some ideas and inspirations how PowerShell can help you to automate your spring cleaning.

And remember, these snippets can have significant effects on your system (such as deleting files or changing system settings). Always test scripts in a safe environment before running them on your main system. Additionally, ensure that you have appropriate backups and recovery tools at hand in case something goes wrong.




Your ultimate PowerShell cheat sheet

Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.

The poster is available for download and in paper form.


PowerShell Poster 2023

Get your poster here!





Related links

Related posts

3 min read

ScriptRunner now available in the Microsoft Azure Marketplace

6 min read

Managing Microsoft Exchange with PowerShell

2 min read

VMUG Webcast: Mastering VMware Management with PowerCLI

About the author: