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

14 min read

Privacy Management with PowerShell – Let's look at the core features of Priva!

All you need to know about Privacy Management, privacy management policies, rules, and subject rights requests.  


4 min read

Let's celebrate System Administrator Appreciation Day!

Every last Friday of July, we get to celebrate System Administrator Appreciation Day, a special day dedicated to...

8 min read

PowerShell 7: What are the pros and cons of switching from Windows PowerShell 5.1?

Windows PowerShell 5.1 is usually preinstalled and the default – is it worth switching to PowerShell 7, or will it lead...

About the author: