⚡ PowerShell 5.1+

Backup Logical Disks List to JSON

Snapshots Logical Disks from Win32_LogicalDisk to a timestamped JSON file for later comparison or auditing.

By WindowsScripting.com · 1.1 KB
Backup-LogicalDiskListToJson.ps1
<#
.SYNOPSIS
    Backs up Logical Disks (Win32_LogicalDisk) to a timestamped JSON file.

.DESCRIPTION
    Queries Win32_LogicalDisk via WMI and writes DeviceID, VolumeName, Size, FreeSpace, DriveType for every item to a
    JSON file, useful as a point-in-time snapshot for change tracking.

.PARAMETER OutputFolder
    Folder to write the JSON snapshot into. Created if it doesn't exist.
    Defaults to the current directory.

.EXAMPLE
    .\Backup-LogicalDiskListToJson.ps1 -OutputFolder C:\Snapshots
#>

[CmdletBinding()]
param(
    [string]$OutputFolder = '.'
)

if (-not (Test-Path $OutputFolder)) {
    New-Item -ItemType Directory -Path $OutputFolder -Force | Out-Null
}

$items = Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object DeviceID, VolumeName, Size, FreeSpace, DriveType
$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$outPath = Join-Path $OutputFolder "logicaldisk-snapshot_$timestamp.json"

$items | ConvertTo-Json -Depth 3 | Out-File -FilePath $outPath -Encoding UTF8

Write-Host "Snapshot of Logical Disks ($($items.Count) items) saved to $outPath" -ForegroundColor Green