Automated Disk Space Monitoring Script

This PowerShell script helps administrators monitor disk space usage on specified drives. By automating this task, the script ensures timely warnings about low disk space, helping to maintain system performance and prevent potential issues.
Step 1: Define Variables for Drive Letters
First, we will set up variables that define which drives we want to monitor for disk space usage.

$drives = @('C:', 'D:', 'E:')
$threshold = 10  # Threshold in GB

In this block, the `$drives` array contains the drive letters to be monitored, and the `$threshold` variable determines the minimum available space (in GB) before an alert will be generated.
Step 2: Check Disk Space for Each Drive
Next, we will loop through each specified drive to check its available disk space using the Get-PSDrive cmdlet.

foreach ($drive in $drives) {
    $diskSpace = Get-PSDrive -Name $drive
    $availableSpaceGB = [math]::round($diskSpace.Free / 1GB, 2)
    if ($availableSpaceGB -le $threshold) {
        Write-Host "Warning: Drive $drive has only $availableSpaceGB GB left."
    } else {
        Write-Host "Drive $drive has $availableSpaceGB GB available."
    }
}

In this block, the script retrieves the free disk space for each drive and then compares it to the predefined threshold. If the available space is less than or equal to the threshold, a warning message is displayed; otherwise, it shows the available space.
Step 3: Log Disk Space Information
For better monitoring and future reference, we can log the disk space information to a text file.

$logFilePath = "C:\DiskSpaceLog.txt"
$currentDateTime = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
foreach ($drive in $drives) {
    $diskSpace = Get-PSDrive -Name $drive
    $availableSpaceGB = [math]::round($diskSpace.Free / 1GB, 2)
    $logMessage = "$currentDateTime - Drive $drive: $availableSpaceGB GB available"
    Add-Content -Path $logFilePath -Value $logMessage
}

This block creates a log entry for each drive, recording the available space along with the timestamp. The log is saved in a text file located at `C:\DiskSpaceLog.txt`.
Step 4: Schedule the Script
To maintain continuous monitoring, its recommended to schedule this script to run at regular intervals using Windows Task Scheduler. This step is not included in the PowerShell script itself but can be accomplished via the Task Scheduler GUI.
By following these steps, this automated disk space monitoring script helps administrators keep track of disk usage efficiently. It acts as a proactive measure to avoid performance issues related to low disk space and ensures system reliability.