ServiceController.GetServices Method
Retrieves all the services on the local computer, except for the device driver services.
Assembly: System.ServiceProcess (in System.ServiceProcess.dll)
Return Value
Type: System.ServiceProcess.ServiceController[]An array of type ServiceController in which each element is associated with a service on the local computer.
| Exception | Condition |
|---|---|
| Win32Exception |
An error occurred when accessing a system API. |
GetServices returns only the non-device driver services and the services that are not drivers from the local computer. To retrieve device driver services, call the GetDevices method. Together, the two methods provide access to all the services on a computer.
The following example uses the ServiceController class to display the services that are running on the local computer.
ServiceController[] scServices; scServices = ServiceController.GetServices(); // Display the list of services currently running on this computer. Console.WriteLine("Services running on the local computer:"); foreach (ServiceController scTemp in scServices) { if (scTemp.Status == ServiceControllerStatus.Running) { // Write the service name and the display name // for each running service. Console.WriteLine(); Console.WriteLine(" Service : {0}", scTemp.ServiceName); Console.WriteLine(" Display name: {0}", scTemp.DisplayName); // Query WMI for additional information about this service. // Display the start name (LocalSytem, etc) and the service // description. ManagementObject wmiService; wmiService = new ManagementObject("Win32_Service.Name='" + scTemp.ServiceName + "'"); wmiService.Get(); Console.WriteLine(" Start name: {0}", wmiService["StartName"]); Console.WriteLine(" Description: {0}", wmiService["Description"]); } }
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
- 8/25/2011
- Yupset
see http://msdn.microsoft.com/en-us/library/system.management.managementobject.aspx
"ManagementObject Class"
- 5/16/2011
- gerry lowry
<#
.SYNOPSIS
This script displays service details
.DESCRIPTION
This script first enumerates all the service controllers (ie services)
running on the local system. For each service, we look into WMI and
get info about that running service.
.NOTES
File Name : Get-ServiceDetails.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/hde9d63a.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-ServiceDetails.ps1'
Services running on the local computer:
Service : AeLookupSvc
Display name: Application Experience
Start name: localSystem
Description: Processes application compatibility cache requests for applications as they are launched
Service : AppHostSvc
Display name: Application Host Helper Service
Start name: LocalSystem
Description: Provides administrative services for IIS, for example configuration history and Applicati
on Pool account mapping. If this service is stopped, configuration history and locking down files or directori
es with Application Pool specific Access Control Entries will not work.
<rest snipped to save space!>
#>
##
# Start of script
##
# Load assembly with ServiceProcess class
$result = [reflection.Assembly]::LoadWithPartialName("System.ServiceProcess")
$Services = [System.ServiceProcess.ServiceController]::GetServices()
# Get WMI Services
$WMIServices = gwmi win32_service
# Display the list of services currently running on this computer.
"Services running on the local computer:"
foreach ($Service in $Services) {
if ($Service.Status -eq [system.ServiceProcess.ServiceControllerStatus]::Running) {
# Write the service name and the display name
# for each running service.
""
" Service : {0}" -f $Service.ServiceName
" Display name: {0}" -f $Service.DisplayName
# query WMI for more info on service
$svc = $wmiServices | where {$_.name -eq $service.servicename}
" Start name: {0}" -f $Svc.StartName
" Description: {0}" -f $Svc.Description
}
}
# End
- 5/22/2010
- Thomas Lee
- 5/22/2010
- Thomas Lee