Determine service status and start or stop them

To get to know the status of service and start or stop a service based on feedback recieved.

Script

<#
.SYNOPSIS
This script can be used to start a service or stop a service.
.DESCRIPTION
This script can be used to start a service or stop a service.
.EXAMPLE
C:\PS> C:\Script\Start_and_Stop_service.ps1
Lists all running and stopped services in local machine.
Enter option 1 to Start a service.
Enter option 2 to Stop a service.
#>

Get-Service | sort status,name
$input=read-host “1.Start a Service 2.Stop a Service ”
if ( $input -eq 1 )
{
$nam = read-host “Service Name to be Started ”
$a = gwmi win32_service | ? {$_.name -eq $nam}
$a
$a.StartService()
$a = gwmi win32_service | ? {$_.name -eq $nam}
$a
}
else
{
if ( $input -eq 2 )
{
$nam = read-host “Service Name to be Stopped ”
$a = gwmi win32_service | ? {$_.name -eq $nam}
$a
$a.StopService()
$a = gwmi win32_service | ? {$_.name -eq $nam}
$a
}
else
{
echo “Invalid Input”
}
}

 

Output
Start and Stop

Leave a Reply