0

Is it possible for me to check if Powershell is installed on a computer using Java. I know how to invoke a powershell script, and I can check whether the OS is Windows XP or later, but I'd prefer to check if powershell.exe exists or something?

Thanks in advance

Andy
  • 3,600
  • 12
  • 53
  • 84

1 Answers1

0

The simple solution is to check if path %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe exists. There is a handy method java.io.File.Exists() for that.

Another way is to query the registry. Should Powershell be present, there will be key HKLM\SOFTWARE\Microsoft\PowerShell. The naive approach will launch reg.exe via Runtime.getRuntime().exec() and read its output. More complex way would be based on JNI/JNA, which on Windows provides access to the registry.

Community
  • 1
  • 1
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Thanks for your answer. The first option would probably be the easiest way, but what if the user has a different version installed?... Would the registry key always be the same regardless of version? – Andy Aug 29 '13 at 19:19
  • @Andy Shouldn't be a problem. MS realized that hard-coding version to path is stupid, so all Powershell versions are [installed in v1.0 directory](http://stackoverflow.com/questions/5415259/why-is-powershell-2-0-installed-in-the-same-location-as-powershell-1-0). – vonPryz Aug 29 '13 at 19:28
  • Ah interesting. I'm glad MS realised that! Thanks for clarifying it. Is there anyway to get the `%WINDIR%` in Java? – Andy Aug 29 '13 at 19:36
  • It's not that hard coding the version was a stupid idea - powershell 2.0 was originally going to be a side by side install. – x0n Aug 30 '13 at 02:12