In this post, we will share a useful PowerShell script designed for effective file and folder management. This script provides functionality to create, copy, and delete files and folders, making it a handy tool for system administrators and power users alike.
powershell
$sourcePath = 'C:\SourceFolder'
$destinationPath = 'C:\DestinationFolder'
if (-Not (Test-Path -Path $destinationPath)) {
New-Item -ItemType Directory -Path $destinationPath
Write-Host "Destination folder created: $destinationPath"
}
Get-ChildItem -Path $sourcePath -File | ForEach-Object {
Copy-Item -Path $_.FullName -Destination $destinationPath
Write-Host "Copied: $($_.Name) to $destinationPath"
}
$thresholdDate = (Get-Date).AddDays(-30)
Get-ChildItem -Path $sourcePath -File | Where-Object { $_.LastWriteTime -lt $thresholdDate } | ForEach-Object {
Remove-Item -Path $_.FullName -Force
Write-Host "Deleted: $($_.Name) from $sourcePath"
}Run it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
Ready when you are.
Try ServerEngine free for 7 days.