PowerShell Services the Basics


    Try{
       
#|||| Gets Service by Name ||||   
        Get-Service -Name Wsearch -ErrorVariable ErrorVar -ErrorAction SilentlyContinue | Select Name, Status

        #|||| Get Service by Name and Starts Service |||        
        Get-Service | Where{$_.Name -eq "WSearch"} | Start-Service -ErrorVariable ErrorVar -ErrorAction SilentlyContinue

        #|||| Get Service by Name and Changes Startup Type |||
        Get-Service | Where{$_.Name -eq "Wsearch"} | Set-Service -StartupType Automatic -ErrorVariable ErrorVar -ErrorAction SilentlyContinue

        #|||| Gets Service by Name and if Stopped Tries to Start it ||||  
        Get-Service -Name WSearch -ErrorVariable  ErrorVar -ErrorAction SilentlyContinue | ?{$_.Status -eq 'Stopped'} | Start-Service 
        

    }Catch{

        $_.Exception

    }


    #Displays any Error Messages
    if($ErrorVar -ne $Null){

    $ErrorVar 

    }
        

2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator