⚡ PowerShell 5.1+

Watch Physical Disk Drives for Changes

Polls Win32_DiskDrive on an interval and prints a line whenever the Physical Disk Drives count changes — a lightweight way to notice new/removed items.

By WindowsScripting.com · 959 B
Watch-DiskDriveChanges.ps1
<#
.SYNOPSIS
    Watches Physical Disk Drives (Win32_DiskDrive) and reports when the item count changes.

.DESCRIPTION
    Polls Win32_DiskDrive every -IntervalSeconds and prints a line whenever the
    number of items returned changes since the last check. Press Ctrl+C
    to stop.

.PARAMETER IntervalSeconds
    How often to poll, in seconds. Defaults to 10.

.EXAMPLE
    .\Watch-DiskDriveChanges.ps1 -IntervalSeconds 30
#>

[CmdletBinding()]
param(
    [int]$IntervalSeconds = 10
)

Write-Host "Watching Physical Disk Drives every $IntervalSeconds second(s). Press Ctrl+C to stop." -ForegroundColor Cyan

$lastCount = -1
while ($true) {
    $count = (Get-CimInstance -ClassName Win32_DiskDrive).Count
    if ($count -ne $lastCount) {
        $timestamp = Get-Date -Format 'HH:mm:ss'
        Write-Host "[$timestamp] Physical Disk Drives count: $count" -ForegroundColor Yellow
        $lastCount = $count
    }
    Start-Sleep -Seconds $IntervalSeconds
}