13 min read
Getting started with PowerShell functions
PowerShell functions are essential tools that enable you to encapsulate code for reuse, making your scripts more...
ScriptRunner Blog
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!
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
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
}
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.
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.
Apr 1, 2025 by Stéphane van Gulick
PowerShell functions are essential tools that enable you to encapsulate code for reuse, making your scripts more...
Feb 24, 2025 by Heiko Brenn
The latest ScriptRunner release enhances Enterprise IT automation with three powerful features: the new Approval Process
Jan 28, 2025 by Jeffery Hicks
Changelogs keep your software updates clear and organized. Learn the best practices for creating and managing them in...
Heiko Brenn is Head of Marketing and Product Marketing Manager. He has been working in the IT industry for more than 25 years and has extensive expertise in email management, security, collaboration, administration, cloud and automation. He has been working with PowerShell since 2010.