How to get all Windows service names starting with a common word? [closed]
Asked Answered
P

4

70

There are some windows services hosted whose display name starts with a common name (here NATION). For example:

  • NATION-CITY
  • NATION-STATE
  • NATION-Village

Is there some command to get all the services like 'NATION-'. Finally I need to stop, start and restart such services using the command promt.

Polyethylene answered 14/12, 2012 at 12:41 Comment(1)
Powershell is installed in your machine ? Server or Workstation? SO ?Cementite
P
158
sc queryex type= service state= all | find /i "NATION"
  • use /i for case insensitive search
  • the white space after type=is deliberate and required
Polyethylene answered 28/12, 2012 at 7:28 Comment(6)
btw, the find is case sensitive, to do a better search, use find /i "Nation" microsoft.com/resources/documentation/windows/xp/all/proddocs/…Triumvirate
remember to use the spaces after type= and state=, I didn't :(Fathom
Note that find will search on service names and also display names - To Filter just service names: sc queryex type= service state= all | find /i "SERVICE_NAME: NATION";).Dollarbird
sc might be shorthand for Set-Content. Use sc.exe if this is the caseValentino
Awesome. Coming from Linux the Windows cmd line landscape makes no sense so it's all magic sauce.Maighdlin
Is there any way to see the services exe paths?Hadleigh
S
28

Using PowerShell, you can use the following

Get-Service | Where-Object {$_.displayName.StartsWith("NATION-")} | Select name

This will show a list off all services which displayname starts with "NATION-".

You can also directly stop or start the services;

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

or simply

Get-Service | Where-Object {$_.displayName.StartsWith("NATION-")} | Restart-Service
Singband answered 14/12, 2012 at 16:27 Comment(2)
Thanks Wimmel for the reply, but instead of shell scripting i am searching to create a simple .cmd file . some command like SC STOP SERVICE_STARTS_WITH("NATION-")Polyethylene
[code]Get-Service -Name *NATION-*[/code] is simpler and better!Decurion
F
0

Another way of doing it, if you don't like the old PowerShell version.

# Create an array of all services running
$GetService = get-service
    
# Iterate throw each service on a host
foreach ($Service in $GetService)
{
    # Get all services starting with "MHS"
    if ($Service.DisplayName.StartsWith("MHS"))
    {
        # Show status of each service
        Write-Host ($Service.DisplayName, $Service.Status, $Service.StartType) -Separator "`t`t`t`t`t|`t"
        
        # Check if a service is service is RUNNING.  
        # Restart all "Automatic" services that currently stopped
        if ($Service.StartType -eq 'Automatic' -and $Service.status -eq 'Stopped' )
        {
            Restart-Service -Name $Service.DisplayName
            Write-Host $Service.DisplayName "|`thas been restarted!"   
        }
    }
}
Fardel answered 17/3, 2022 at 16:53 Comment(0)
I
-3

Save it as a .ps1 file and then execute

powershell -file "path\to your\start stop nation service command file.ps1"

Intonate answered 10/5, 2016 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.