🟣 C# / .NET 6+

Get Services Report (C#)

Queries Win32_Service via WMI (System.Management) and prints Services to the console.

By WindowsScripting.com · 1 KB
GetServiceReport.cs
// GetServiceReport.cs
//
// Reports Services via WMI (Win32_Service).
//
// Requires the System.Management package:
//   dotnet new console -o ServiceReport
//   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, DisplayName, State, StartMode, Status FROM Win32_Service");
int count = 0;

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

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