Friday, August 10, 2012

Enabling and starting a service in Powershell

Just a quick post to show you my new preferred method to enable and start a service. Until yesterday I opened a RDP connection to the remote server, then I fired the services.msc console and selected the service to enable, double-clicked on it and modified its settings.

Today things have changed and Powershell does it better. Just fire a Powershell session and execute the following cmdlet:

get-service -computername yourserver -name "windows installer" | set-service -StartupType "automatic"
get-service -computername yourserver -name "windows installer" | start-service
get-service -computername yourserver -name "windows installer"

Status   Name               DisplayName
------   ----               -----------
Running  MSIServer          Windows Installer

Of course you can use aliases to be quicker. Start by finding them with:

Get-Alias | ?{$_.definition -like "*service*"}

CommandType     Name            Definition
-----------     ----            ----------
Alias           gsv             Get-Service
Alias           sasv            Start-Service
Alias           spsv            Stop-Service

The previous command lines can now be rewritten like this:

gsv -computername yourserver -name "windows installer" | set-service -StartupType "automatic"
gsv -computername yourserver -name "windows installer" | sasv
gsv -computername yourserver -name "windows installer"

Status   Name               DisplayName
------   ----               -----------
Running  MSIServer          Windows Installer

Hope this helps.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...