Call Powershell from VBA RRS feed

  • Question

  • Hi

    I tried your Ed Wilsons script in vbscript:

    option explicit

    dim objShell


    Wscript.Echo "Starting"

    Set objShell = CreateObject("Wscript.shell")

    objShell.run("powershell -executionpolicy bypass -noexit -File D:\IT\PS1\HelloWorld.ps1")

    Wscript.Echo "Done"


    It worked fine.


    I tried to start Powershell from  Excel vba:

    Option Explicit


    Private Sub CommandButton1_Click()

    Dim sCommand As String, sOutput As String

    Dim oWshShell As Object, oWshShellExec As Object


    sCommand = "Powershell -executionpolicy bypass -noexit "

    Debug.Print sCommand

    Set oWshShell = CreateObject("WScript.Shell")

    Set oWshShellExec = oWshShell.Exec(sCommand)

    sOutput = oWshShellExec.StdOut.ReadAll

    MsgBox (sOutput)

    Set oWshShellExec = Nothing

    Set oWshShell = Nothing

    End Sub


    But it do not work.

    ProcMon shows that Powersheel starts and immediately stops.

    “-noexit” has no effect.


    How do I start Powershell with -noexit from Excel vba?




    Jan Andersen

    Boelsvang 26

    DK-2970 Hoersholm



    Mobile:          +45 2022 1196

    E-mail:          janand@janand.dk

    www:             www.janand.dk


    • Moved by Bill_Stewart Monday, April 30, 2018 9:39 PM This is not MS Office development forum
    Wednesday, February 28, 2018 2:05 PM

All replies

  • You need to post you issue in an Excel VBA forum.  VBA has a different way of executing an external process and this is not an Excel forum.


    Wednesday, February 28, 2018 3:26 PM
  • I know. But as I wrote ProcMon shows that Powershell is starting but exits right away dispite -noexit. 
    Wednesday, February 28, 2018 3:31 PM
  • This is not a PowerShell issue.  It is an Excel VBA issue.


    Wednesday, February 28, 2018 3:32 PM
  • Here is a link to the VBA method for shell execution.


    Post further questions in the Excel VBA forum.


    Wednesday, February 28, 2018 3:34 PM