🟣 C# / .NET 6+

Backup Physical Memory Modules to JSON (C#)

Snapshots Physical Memory Modules (Win32_PhysicalMemory) to a timestamped JSON file via System.Management and System.Text.Json.

By WindowsScripting.com · 1.2 KB
BackupPhysicalMemoryListToJson.cs
// BackupPhysicalMemoryListToJson.cs
//
// Snapshots Physical Memory Modules (Win32_PhysicalMemory) to a timestamped JSON file.
//
// Requires the System.Management package:
//   dotnet new console -o BackupPhysicalMemory
//   dotnet add package System.Management
//   (replace the generated Program.cs with this file, then dotnet run)

using System;
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Text.Json;

var searcher = new ManagementObjectSearcher("SELECT BankLabel, Capacity, Speed, Manufacturer FROM Win32_PhysicalMemory");
var results = new List<Dictionary<string, object?>>();

foreach (ManagementObject item in searcher.Get())
{
    var row = new Dictionary<string, object?>();
    row["BankLabel"] = item["BankLabel"];
        row["Capacity"] = item["Capacity"];
        row["Speed"] = item["Speed"];
        row["Manufacturer"] = item["Manufacturer"];
    results.Add(row);
}

string timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss");
string outPath = $"physicalmemory-snapshot_{timestamp}.json";
File.WriteAllText(outPath, JsonSerializer.Serialize(results, new JsonSerializerOptions { WriteIndented = true }));

Console.WriteLine($"Snapshot of Physical Memory Modules ({results.Count} items) saved to {outPath}");