11 min read
Graph PowerShell SDK – Part 1 of our Graph series
Users will encounter one or two hurdles when they start using Graph. Damian Scoles wrote three articles provides help...
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.
Oct 8, 2024 by Damian Scoles
Users will encounter one or two hurdles when they start using Graph. Damian Scoles wrote three articles provides help...
Oct 2, 2024 by Heiko Brenn
Managing Active Directory (AD) is a crucial yet time-consuming task for IT administrators. The constant need to manage...
Sep 30, 2024 by Frank Kresse
We have just released our latest ScriptRunner update, version 7.1, packed with powerful new features aimed at making IT...
Heiko Brenn is Product Marketing Manager and responsible for the ScriptRunner marketing team. 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.