There are two separate issues with the path logic.
-
When witadmin.exe is in the path, Test-Path will fail, so it will still throw even though it's valid and can do the work.
-
In 32-bit PowerShell processes, $env:ProgramFiles resolves to C:\Program Files (x86), so $VSDirectories is built with the wrong path.