Powershell script

Is it possible to check service under services.msc is started using Powershell, batch?

If yes can i automatic start using the Powershell or batch.
Nandha Kumar BAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
Batch script example from here shown below.

@ECHO OFF
SET SvcName=ServiceName
 
SC QUERYEX "%SvcName%" | FIND "STATE" | FIND /v "RUNNING" > NUL && (
    ECHO %SvcName% is not running 
    ECHO START %SvcName%
 
    NET START "%SvcName%" > NUL || (
        ECHO "%SvcName%" wont start 
        EXIT /B 1
    )
    ECHO "%SvcName%" is started
    EXIT /B 0
) || (
    ECHO "%SvcName%" is running
    EXIT /B 0
)

Open in new window


»bp
0
 
oBdACommented:
Powershell "properly", using the service's short name:
Get-Service BITS | ? {$_.Status -ne 'Running'} | Start-Service

Open in new window

Using the service's display name:
Get-Service -DisplayName 'Background Intelligent Transfer Service' | ? {$_.Status -ne 'Running'} | Start-Service

Open in new window

Since PowerShell doesn't care whether the service is already running, you can just leave out all the unnecessary stuff:
Start-Service BITS

Open in new window

Or
Start-Service -DisplayName 'Background Intelligent Transfer Service'

Open in new window

2
 
Nandha Kumar BAuthor Commented:
Thank you it is working as expected
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.