Recently I needed to check if IIS was installed on a machine using Powershell.
The obvious way of doing this is:
$iis = Get-Service W3SVC
if ($iis -eq $NULL)
{
"IIS not found"
}
The problem with this is that it does not really work, since the first line will crash if the service is not found.
The solution for this is to make it fail silently.
$iis = Get-Service W3SVC -ErrorAction SilentlyContinue
if ($iis -eq $NULL)
{
"IIS not found"
}
This makes it fail silently, and the code works as expected!