PowerShell Script to Restart the Web Management Service

It’s been over a year since I last wrote an article partly because I didn’t know what I wanted to blog about and partly because of the lack of time. Well, the other day I found myself working on a problem to simplify restarting the Web Management Service on a remote machine when it gets stuck (i.e. hangs and goes into a Stopping state) and felt that the script might be useful to other Windows system administrators out there. So here it is:

$ComputerName = Read-Host -Prompt 'Enter computer name or IP address: '
$Username = Read-Host -Prompt 'Enter your username: '
$Password = Read-Host -Prompt 'Enter your password: '
$ProcessId = Get-Process -ComputerName "$ComputerName" WMSvc | Select -Expand Id
taskkill.exe /S "$ComputerName" /U "$Username" /P "$Password" /IM "$ProcessId" /F

All you need to do is enter the computer name or IP address of the remote computer and your Active Directory Domain login credentials after running the script, and it will do the rest.