Automating the world one-liner at a time…
"How do we programmatically detect that PowerShell is installed ?" - This question has started coming up frequently as more folks (both internally and externally) have started to build applications with PowerShell support. As we approach Win7/W2K8-R2 release, which will include PowerShell 2.0 in-box, it is pertinent to have the right guidance out for PowerShell detection logic. If you're writing an installer which relies on PowerShell presence, not only do you need to detect whether PowerShell is installed, but also what version of PowerShell is installed.
Remember:
Hemant Mahawar [MSFT]Program ManagerWindows PowerShell
<Updated 6/26 to fix the spelling of PowerShell>
And what should we use to test it VBScript? LOLL
Did you really spell PowerShell wrong in the article title!!
@Daniel - thanks. Fixed now.
Enjoy!
Jeffrey Snover [MSFT]
Distinguished Engineer
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
FYI, the ps installer won't set HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\Insall = 1... so if your app is 32 bit and running on 64 bit windows then this method won't work.
How do we detect PowerShell 2.0 (but not 1.0) in a manner that won't break when PowerShell 3.0 is released?
Is there a simple variable that can be used for version detection? I'm thinking of something like:
if ( $psVersion -lt 2 )
{ 'Powershell version must be 2 or greater' }
else
{ 'Processing x' }
I see the team have addressed this in version 2 with the $psversiontable structure.
The foillowing code should do the trick:
if ( $psversiontable ) { 'v2+' } else { 'v1' }
How is PS removed from win2003? KB 139 & 141 are not found in add/remove updates and 2003-sp2 is removed but PS aritfacts remain in registry and in system32 folder.
How about you write a script that tells us which version of powershell to install via OS and then tries to install it. 4 tries of my OS (windows 7 64 bit) and each one gives me the 'this install is not applicable to your system' everytime.
You know what doesn't give me this error? EVERY OTHER scripting language!