68

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chandan Kumar
  • 2,617
  • 2
  • 19
  • 20

4 Answers4

156
sc queryex type= service state= all | find /i "NATION"
  • use /i for case insensitive search
  • the white space after type=is deliberate and required
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Chandan Kumar
  • 2,617
  • 2
  • 19
  • 20
  • 3
    btw, the find is case sensitive, to do a better search, use find /i "Nation" http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/find.mspx?mfr=true – fedmich Oct 04 '13 at 03:29
  • 3
    remember to use the spaces after type= and state=, I didn't :( – Guy Lowe Nov 19 '14 at 04:08
  • 4
    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"`;). – shA.t Sep 19 '16 at 08:37
  • 1
    `sc` might be shorthand for `Set-Content`. Use `sc.exe` if this is the case – Brad Dec 27 '17 at 20:35
  • 1
    Awesome. Coming from Linux the Windows cmd line landscape makes no sense so it's all magic sauce. – BaseZen Mar 08 '18 at 18:45
  • Is there any way to see the services exe paths? – Shayan May 18 '19 at 14:39
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
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
wimh
  • 15,072
  • 6
  • 47
  • 98
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!"   
        }
    }
}
Serge V.
  • 3,377
  • 3
  • 20
  • 28
-3

Save it as a .ps1 file and then execute

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

o-90
  • 17,045
  • 10
  • 39
  • 63