69

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.

1
  • Powershell is installed in your machine ? Server or Workstation? SO ?
    – Kiquenet
    Nov 27, 2015 at 8:18

4 Answers 4

157
sc queryex type= service state= all | find /i "NATION"
  • use /i for case insensitive search
  • the white space after type=is deliberate and required
6
  • 3
    btw, the find is case sensitive, to do a better search, use find /i "Nation" microsoft.com/resources/documentation/windows/xp/all/proddocs/…
    – fedmich
    Oct 4, 2013 at 3:29
  • 3
    remember to use the spaces after type= and state=, I didn't :(
    – Guy Lowe
    Nov 19, 2014 at 4: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, 2016 at 8:37
  • 1
    sc might be shorthand for Set-Content. Use sc.exe if this is the case
    – Brad
    Dec 27, 2017 at 20:35
  • 1
    Awesome. Coming from Linux the Windows cmd line landscape makes no sense so it's all magic sauce.
    – BaseZen
    Mar 8, 2018 at 18:45
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
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-") Dec 15, 2012 at 17:47
  • [code]Get-Service -Name *NATION-*[/code] is simpler and better! Sep 23, 2019 at 12:54
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!"   
        }
    }
}
-3

Save it as a .ps1 file and then execute

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.