⚡ PowerShell 5.1+

Watch Sound Devices for Changes

Polls Win32_SoundDevice on an interval and prints a line whenever the Sound Devices count changes — a lightweight way to notice new/removed items.

By WindowsScripting.com · 946 B
Watch-SoundDeviceChanges.ps1
<#
.SYNOPSIS
    Watches Sound Devices (Win32_SoundDevice) and reports when the item count changes.

.DESCRIPTION
    Polls Win32_SoundDevice 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-SoundDeviceChanges.ps1 -IntervalSeconds 30
#>

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

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

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