Home > Net >  Powershell command to set service 'log on' to 'LocalSystem'
Powershell command to set service 'log on' to 'LocalSystem'

Time:10-31

What I want to do: Set 'Log on' to 'LocalSystem' on all windows services where the display name starts with 'CTM_'

I have found a powershell command to stop/start all services where the name starts with 'CTM_':

Get-Service | Where-Object {$.displayName.StartsWith("CTM")} | Start-Service

And I have found a powershell commando to set a specific service 'log on' to 'LocalSystem':

sc.exe config "ServiceName" obj="Localsystem"

What I am strugling with, is to find a command to run the last powershell command on all windows services where the display name starts with a give text, for example 'CTM_'

Image

CodePudding user response:

You can use the CIM cmdlets, e.g.

#Get all services where the name starts with 'CTM_' and the currently configured starName is not localSystem, set startname to localsystem

get-ciminstance -query "select name from win32_service where name like 'CTM_%' and startname <> 'localsystem'" | Invoke-CimMethod -Name change -Arguments @{startname='localsystem'}
  • Related