Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

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.

1
2
3
4
5
# 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.

1
2
# 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.

1
2
# 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 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.

1
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.