Disk Space Cleanup and Report Script
This PowerShell script helps maintain system performance by identifying large files and folders on specified drives. It not only lists these files but also offers an option to delete them, making it an effective tool for disk space management.
Step 1: Define Variables for Path and Size Threshold
First, we need to set the drive paths and the size threshold for listing files. This is critical for targeting the cleanup operation effectively.
# Define the path to search for large files $searchPath = "C:\" # Set the minimum file size threshold in MB $sizeThresholdMB = 100 $sizeThresholdBytes = $sizeThresholdMB * 1MB
In this code block, we define `$searchPath` as the directory to search (in this case, the C drive) and set `$sizeThresholdMB` to filter files larger than 100 MB. The size is converted into bytes for comparison.
Step 2: Retrieve Large Files
Next, we will gather a list of files larger than the defined size threshold using the `Get-ChildItem` cmdlet, which retrieves the files from the specified path.
# Get files larger than the threshold in the specified path $largeFiles = Get-ChildItem -Path $searchPath -Recurse -File | Where-Object { $_.Length -gt $sizeThresholdBytes }
Here, the script uses `Get-ChildItem` with the `-Recurse` parameter to search through all subdirectories for files. The `Where-Object` clause filters out the files that exceed the defined size.
Step 3: Display the List of Large Files
Once we have the large files, we will output them in a readable format with relevant information such as name, size, and path.
# Display file details: Name, Size (MB), and Path $largeFiles | Select-Object Name, @{Name="Size (MB)"; Expression={[math]::round($_.Length / 1MB, 2)}}, FullName | Format-Table -AutoSize
This block formats the output to display only the necessary details: the name of the file, its size in MB (rounded to two decimal places), and its full path. The `Format-Table -AutoSize` ensures the output is neatly aligned.
Step 4: Prompt User for Deletion Option
After displaying the large files, we will ask the user if they want to delete any of these files. This step is crucial for preventing accidental data loss.
# Ask user for deletion $deleteFiles = Read-Host "Do you want to delete any of these files? (Y/N)" if ($deleteFiles -eq 'Y') { foreach ($file in $largeFiles) { # Prompt for each file deletion $confirmDelete = Read-Host "Do you want to delete $($file.FullName)? (Y/N)" if ($confirmDelete -eq 'Y') { Remove-Item -Path $file.FullName -Force Write-Host "Deleted: $($file.FullName)" } } } else { Write-Host "No files will be deleted." }
In this section, if the user responds with ‘Y’ to the deletion prompt, the script will iterate through the list of large files and confirm each deletion. The `Remove-Item` cmdlet is used to delete the file if confirmed.
Step 5: Completion Message
Finally, we will provide a completion message for clarity on the outcome of the script, whether files were deleted or not.
Write-Host "Disk space cleanup operation completed."
This concluding step gives feedback to the user, indicating that the operation has finished, providing closure to the script execution.
By employing this script, system administrators can effectively manage disk space by identifying and removing large files, thus enhancing system performance and preventing potential storage issues. This proactive approach is essential for maintaining a healthy IT environment.