begin { function Uninstall-IPerf3 { [CmdletBinding()] param ( [Parameter()] [String] $DestinationPath = 'C:\Program Files\iperf3' ) begin { $Service = Get-Service -Name iperf3server -ErrorAction SilentlyContinue } process { if ($null -eq $Service) { Write-Host "Service is not installed" } else { try { & "C:\Program Files\nssm\win64\nssm.exe" remove iperf3server confirm } catch { Write-Error "Failed to uninstall service. $_" } } # Remove Program Files folder Remove-Item -Path $DestinationPath -Recurse -Force # Remove from PATH $RegexPath = $DestinationPath -replace '\\', '\\' [Environment]::SetEnvironmentVariable("Path", ($env:Path -replace ";$RegexPath\\",''), [System.EnvironmentVariableTarget]::Machine) } } } process { if (-not (Test-Path (Join-Path -Path $DestinationPath -ChildPath 'iperf3.exe'))) { Write-Host 'iPerf3 is not installed.' } else { Uninstall-IPerf3 } }