0

Let's say I have a powershell script sorta like this:

'Hey'

Which I want to call in VBA sorta like this:

Sub CallPowerShell()

    Call Shell("powershell.exe -ExecutionPolicy Bypass C:\Users\chm\Documents\5.ps1")

End Sub

I know this works as a console window briefly pops up with the word 'Hey' in it. What I really want however, is to use this string in my VBA code.

Is there a simple way how to get a return value from a powershell script through VBA?

Ideally it would be something like this:

Dim x As String

Sub CallPowerShell()

    x = Shell("powershell.exe -ExecutionPolicy Bypass C:\Users\chm\Documents\5.ps1")

End Sub

But that just sets x to seemingly random numbers like 70640 or 30960.

I could have the powershell script write the value I want to a file and then read the file in VBA but it'd be really nice if I could skip that so I figured I'd ask here.

Der Kejser
  • 63
  • 10
  • 1
    Possible duplicate of [How to get a variable from cmd and show it in vbscript - Vbscript](https://stackoverflow.com/questions/48845763/how-to-get-a-variable-from-cmd-and-show-it-in-vbscript-vbscript) – Tomalak Aug 27 '18 at 07:58
  • There are many answers that show you how to use the `WScript.Shell` object from VBA (or VBS, does not make much difference). Look at a few of them. – Tomalak Aug 27 '18 at 08:03

0 Answers0