Use PowerShell to Delete Files for more space
Use PowerShell to Delete Files for more space

Use PowerShell to Delete Files for more space

Un problema che assilla molti utenti è quello di non avere spazio sul proprio Hard Disk (HD). Problema molto frequente se si usano HD di server usati per lo sviluppo software. Come fare allora per avere uno strumento facile da usare per liberare spazio?

I software in commercio, io consiglio CCleaner, sono utilissimi per liberare spazio occupato dai software ufficiali (cartelle temporanee, file lasciati dagli aggiornamenti windows, etc.).

Io vi propongo uno script PowerShell che potete liberamente personalizzare.

 

 

Ecco il codice PowerShell fatto da me:

 

function clear-all-event-logs ()
{
   $logs = Get-EventLog -List | ForEach-Object {$_.Log}
   $logs | ForEach-Object {Clear-EventLog -LogName $_ }
   #Get-EventLog -list
}

function deleteFiles($path)
{
    $Hoursback = "-2"
    $CurrentDate = Get-Date
    $DatetoDelete = $CurrentDate.AddHours($Hoursback)
    Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Recurse -force
    #Get-ChildItem $Path -Recurse | Remove-Item -Recurse -force
}
function deleteFiles2($path, $Daysback)
{   
    $CurrentDate = Get-Date
    $DatetoDelete = $CurrentDate.AddDays($Daysback)
    Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Recurse -force
    #Get-ChildItem $Path -Recurse | Remove-Item -Recurse -force
}

cls;

Write-Host "Script for finding free space!!!"
Write-Host "You are running under this version of PowerShell:"
$PSVersionTable.PSVersion
Write-Host "-----------------------------------------------"
Write-Host "    1. Delete files in the recyle bin of the users..."
#Clear-RecycleBin -Confirm:$false
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -Wait -verb runas -ArgumentList {/c del /s /q %systemdrive%\$Recycle.bin}
Write-Host "    1. DONE!!!"
Write-Host "    2. Delete Event Log Files..."
clear-all-event-logs
Write-Host "    2. DONE!!!"

#Write-Host "    3. Delete Files..."
#Write-Host "        3.1 Delete Files C:\Builds\157\MrCredit..."
#deleteFiles "my path to folder to clean"

#-10 means: clean all file with a create date <= Now - 10 days
#Write-Host "        3.4 Delete Files C:\Log..."
#deleteFiles2 "my path to folder to clean" "-10"


Write-Host "    3. DONE!!!"
pause

 

 

 

Buon Lavoro!