Chandan Kumar
Chandan Kumar

Reputation: 2727

How to get all Windows service names starting with a common word?

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

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.

Upvotes: 73

Views: 217863

Answers (4)

Serge V.
Serge V.

Reputation: 3623

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!"   
        }
    }
}

Upvotes: 0

Chandan Kumar
Chandan Kumar

Reputation: 2727

sc queryex type= service state= all | find /i "NATION"
  • use /i for case insensitive search
  • the white space after type=is deliberate and required

Upvotes: 161

wimh
wimh

Reputation: 15232

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

Upvotes: 29

blairstark
blairstark

Reputation: 1

Save it as a .ps1 file and then execute

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

Upvotes: -3

Related Questions