🟣 C# / .NET 6+

Get Battery Info Report (C#)

Queries Win32_Battery via WMI (System.Management) and prints Battery Info to the console.

By WindowsScripting.com · 1.1 KB
GetBatteryReport.cs
// GetBatteryReport.cs
//
// Reports Battery Info via WMI (Win32_Battery).
//
// Requires the System.Management package:
//   dotnet new console -o BatteryReport
//   dotnet add package System.Management
//   (replace the generated Program.cs with this file, then dotnet run)
// On .NET Framework, System.Management is already available — just
// add a reference to it in your project.

using System;
using System.Management;

var searcher = new ManagementObjectSearcher("SELECT Name, EstimatedChargeRemaining, BatteryStatus, Chemistry FROM Win32_Battery");
int count = 0;

foreach (ManagementObject item in searcher.Get())
{
    Console.WriteLine($"Name: {item["Name"]}");
    Console.WriteLine($"EstimatedChargeRemaining: {item["EstimatedChargeRemaining"]}");
    Console.WriteLine($"BatteryStatus: {item["BatteryStatus"]}");
    Console.WriteLine($"Chemistry: {item["Chemistry"]}");
    Console.WriteLine(new string('-', 40));
    count++;
}

if (count == 0)
{
    Console.WriteLine("No Battery Info found.");
}
else
{
    Console.WriteLine($"\n{count} Battery Info found.");
}