PowerShell: Get Service Status

Determine service status and start or stop them

Get service status and start or stop a service based on feedback received.

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”
}
}

 




    • Related Articles

    • Determine whether a machine is Laptop or not

      Administrators managing an enterprise with thousands of computers need to ascertain occasionally whether a particular computer is a laptop or a desktop before pushing out updates and applications. However, doing so manually is time-consuming. This is ...
    • Determine version of Exe or Dll

      IT administrators need to compare the version information of the EXE or DLL files. This can be critical because, the version numbers indicate whether a software patch has been applied or not. However sifting through multiple version numbers of all ...
    • How to Find and Kill Running Processes with PowerShell

      Heavy hardware resource utilization on end-user machines will lead to reduced productivity by hindering users from getting their job done. In this article, we'll discuss how administrators can identify and kill memory-hogging processes. The following ...
    • How to retrieve and analyze Microsoft 365 usage data with PowerShell

      Retrieving and analyzing Microsoft 365 usage data with PowerShell can provide valuable insights into how your organization is using Microsoft 365 services, which can help you optimize your environment and improve user productivity. In this ...
    • PowerShell for AD user reports

      Real-time insights on user account status and activity can help AD  administrators manage accounts better. Many administrators use Microsoft's PowerShell scripts to generate Active Directory reports  and pull detailed information. Below are some ...