🟣 C# / .NET 6+
Backup Local Groups to JSON (C#)
Snapshots Local Groups (Win32_Group) to a timestamped JSON file via System.Management and System.Text.Json.
BackupGroupListToJson.cs
// BackupGroupListToJson.cs
//
// Snapshots Local Groups (Win32_Group) to a timestamped JSON file.
//
// Requires the System.Management package:
// dotnet new console -o BackupGroup
// 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 Name, Description, Domain FROM Win32_Group");
var results = new List<Dictionary<string, object?>>();
foreach (ManagementObject item in searcher.Get())
{
var row = new Dictionary<string, object?>();
row["Name"] = item["Name"];
row["Description"] = item["Description"];
row["Domain"] = item["Domain"];
results.Add(row);
}
string timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss");
string outPath = $"group-snapshot_{timestamp}.json";
File.WriteAllText(outPath, JsonSerializer.Serialize(results, new JsonSerializerOptions { WriteIndented = true }));
Console.WriteLine($"Snapshot of Local Groups ({results.Count} items) saved to {outPath}");