Windows Services (PowerShell)

This example retrieves all of the services on a computer

Get-Service
Get-WMIObject Win32_Service

Get the service called ‘LanmanServer’

Get-WMIObject Win32_Service -filter “Name=’LanmanServer'”
 

Get the services that are currently ‘Running’

Get-Service | Where-Object {$_.Status -eq “Running”}
 

Get the services that are currently ‘Stopped’

Get-Service | Where-Object {$_.Status -eq “Stopped”}
 

Get the services that have their startup mode set to ‘Automatic’

Get-WMIObject Win32_Service | Where-Object {$_.StartMode -eq “Auto”}
 

Get the services that have their startup mode set to ‘Automatic’ and are ‘Stopped’

Get-WMIObject Win32_Service | Where-Object {$_.StartMode -eq “Auto” -and $_.State -eq “Stopped”}
 

Get the services that have a name that is like ‘vm’ and change their startup mode to ‘Manual’

Get-WMIObject Win32_Service | Where-Object {$_.name –like “vm*”} | Foreach {$_.changestartmode(“Manual”)}
 

Stop a Service

Stop-Service tabletinputservice

(Get-WmiObject -computer $servername Win32_Service -Filter “Name=’$service'” -Credential $Cr).InvokeMethod(“StopService”,$null)
 

Start a Service

Start-Service tabletinputservice

(Get-WmiObject -computer $servername Win32_Service -Filter “Name=’$service'” -Credential $Cr).InvokeMethod(“StartService”,$null)
 

Restart a Service

Restart-Service tabletinputservice
 

Suspend a Service

Suspend-Service tabletinputservice
 

Resume a Suspended Service

Resume-Service tabletinputservice
 

Get Services that are currently Running and that are able to be Paused

Get-Service -ComputerName $computer | Where-Object {$_.CanPauseAndContinue -eq ‘True’ -and $_.Status -eq ‘Running’}
 

Get the dependencies of a Service

Get-Service workstation | select DependentServices
 

Get the Service that the selected service depends on

Get-Service workstation | select ServicesDependedOn
 

Get-WmiObject Win32_Service -computer $computer | ? {($_.StartName -ne “LocalSystem”) -and ($_.StartName -ne “NT AuthorityNetworkService”) -and ($_.StartName -ne “NT AuthorityLocalService”) -and ($_.StartName -ne “NT AuthorityLocal Service”)} | Select StartName, Name, DisplayName

 
Comments

No comments yet.

Leave a Reply