   #Functions
function StopService{
    param($ServiceName)
    if ((Get-Service $ServiceName -ErrorAction SilentlyContinue).StartType -eq 'Automatic') {
     write-output "Stopping $ServiceName"
     # the svc.faxagent seems to be unusually stubbern when it comes time to stop.
     $arrService = Get-Service -Name "$ServiceName" -ErrorAction SilentlyContinue
         if($arrService -ne $null)  {
         # service exists see if its running
             if ($arrService.Status -eq "Running"){
                 Stop-Service $ServiceName -Force
             }
         }
     }
} # end of StopService

if (-NOT (Test-Path 'HKLM:SYSTEM\CurrentControlSet\Services\FXC6.ServicesAPI')){
    return 'No Faxcore eV6 Software detected!  ABORTING'
    }   

    #Where is faxcore Installed
#look in the registry for where the dispatcher service starts.
$FCPath = split-path(split-path (Get-ItemProperty "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\FXC6.ServicesAPI" -Name "ImagePath").ImagePath)

   
    #Stop services
StopService "fxc6.dispatchagent"
StopService "fxc6.faxagent"
StopService "fxc6.renderagent"
StopService "fxc6.filegateway"
StopService "fxc6.reportAPI"
StopService "fxc6.servicesAPI"
StopService "fxc6.smtpgatewayagent"
StopService "SQL Server (FXCDB)"

#did they really stop?
    $Services = Get-WmiObject -Class win32_service -Filter "state = 'stop pending'"
    if ($Services) {
        foreach ($service in $Services) {
        try {
                Stop-Process -Id $service.processid -Force -PassThru -ErrorAction Stop
            }
            catch {
                Write-Warning -Message " Error. Error details: $_.Exception.Message"
            }
        }
    }

     if (get-website | where-object { $_.name -eq 'FaxCore' }) {
          Remove-WebAppPool FaxCoreEv6
          Remove-Item IIS:\Sites\faxcore -Force -Recurse
        
   sc.exe delete FXC6.DispatchAgent 
    sc.exe delete FXC6.FaxAgent 
    sc.exe delete FXC6.FileGateway 
    sc.exe delete FXC6.RenderAgent 
    sc.exe delete FXC6.ReportAPI 
    sc.exe delete FXC6.SMTPGatewayAgent
    sc.exe delete FXC6.ServicesAPI

}

Remove-Item -Recurse -Force $FCPath