Determine Powershell.exe version (1 Viewer)

supmktg

Registered User.
Local time
Today, 00:32
Joined
Mar 25, 2002
Messages
360
I'm using Powershell.exe to access data on a Sharepoint server. The Powershell script fails if the installed version of Powershell is less than 3.

I'm trying to run the following vba code to determine which version of Powershell.exe is installed, so I can advise the user to update their Powershell:

Code:
Dim intVers As Integer
intVers = Shell("Powershell.exe -executionpolicy bypass $PSVersionTable.PSVersion.Major")
MsgBox intVers

When I manually run the cmdlet "$PSVersionTable.PSVersion.Major" in Powershell, I get the correct result ( 2 or 3 or 4). But when I run it within the above vba code, I get a random set of 3 or 4 numbers (different each time I run it).

Can anyone tell me why this vba code is not working properly? Or, can someone suggest another way to determine the installed version of Powershell?

Thanks,
Sup
 

MarkK

bit cruncher
Local time
Yesterday, 22:32
Joined
Mar 17, 2004
Messages
8,186
It looks like the code you posted returns the result of the call to the Shell() method. From VBA help under "Shell" . . .
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.
 

supmktg

Registered User.
Local time
Today, 00:32
Joined
Mar 25, 2002
Messages
360
Hi Mark,

Thanks for the response.

I've been to the link you suggested: http://blogs.technet.com/b/heyscriptingguy/archive/2009/01/05/how-do-i-check-which-version-of-windows-powershell-i-m-using.aspx

Unfortunately, those powershell script examples also return the Task ID instead of the Powershell vesion.

Although I can't understand the logic behind returning the TaskID, I've accepted that I can't simply return the version to an Access message box. So I created the following Powershell script that includes it's own message box:

Code:
$wshell = New-Object -ComObject Wscript.Shell
$PSVersion = $PSVersionTable.PSVersion.Major
$wshell.Popup($PSVersion,0,"PowerShell Version")

Although it is uncommon to run PowerShell via VBA, I'm hoping this post may help someone in the future.

Thanks,
Sup
 

MarkK

bit cruncher
Local time
Yesterday, 22:32
Joined
Mar 17, 2004
Messages
8,186
Right, but using that info you can then read that registry setting using WMI like . . .
Code:
Sub Test187461294683()
    Const HKEY_LOCAL_MACHINE = &H80000002
    Const PS_KEY As String = "SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"
    
    Dim oReg As Object
    Dim sComputer As String
    Dim sValue As String
    
    sComputer = "."
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\default:StdRegProv")
    oReg.GetStringValue HKEY_LOCAL_MACHINE, PS_KEY, "RuntimeVersion", sValue
    
    MsgBox sValue
      
End Sub
Hope that helps,
 

supmktg

Registered User.
Local time
Today, 00:32
Joined
Mar 25, 2002
Messages
360
Hi Mark,

This helps a lot! Now that I can put the result into a variable, I can silently test the Powershell version and only inform the user if the version is inadequate.

BTW, on my machine the KEY "SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine" shows a RuntimeVersion = 2. However, there is another KEY "SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine" that properly shows a RuntimeVersion = 4. I guess I'll need to loop through registry KEYS 1,2,3, etc to see if they exist and then grab the one with the largest RuntimeVersion.

This is extremely helpful!

Thanks so much,
Sup
 

MarkK

bit cruncher
Local time
Yesterday, 22:32
Joined
Mar 17, 2004
Messages
8,186
Yeah, maybe there's a better registry key to read. I just cracked open my registry here, and I think the PowerShellVersion looks more promising than RuntimeVersion.

But this code can return any registry (string) value. Google for other methods, like GetDWORDValue, and so on . . .

But glad you like it, cheers,
 

Users who are viewing this thread

Top Bottom