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