1

Since a time, I was using this code and it worked before

Sub Call_PowerShell_Script_File_From_Excel_VBA()
'Windows PowerShell ISE >> New Script >> Create 'Hello World.ps1'
'    echo "Hello World"
'    $x = 1 + 1
'    echo $x
'----------------------------------------------------------------
Dim wshShell        As Object
Dim wshShellExec    As Object
Dim strCommand      As String
Dim strOutput       As String

strCommand = "Powershell.exe -File ""C:\Users\Future\Desktop\Hello World.ps1"""
Set wshShell = CreateObject("WScript.Shell")
Set wshShellExec = wshShell.Exec(strCommand)
strOutput = wshShellExec.StdOut.ReadAll

MsgBox strOutput
End Sub

I was working on Windows 7 and the code was with no problem. After installing Windows 10, I found the code not working and I got blank message with no output Any ideas ??

** When testing Tim's code I got this message

StdOut:       
StdErr:       File C:\Users\Future\Desktop\Hello World.ps1 cannot be loaded 
because running scripts is disabled on this system. For 
more information, see about_Execution_Policies at 
https:/go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo          : SecurityError: (:) [], 
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess
2
  • 1
    Can you run that script manually? Commented Mar 19, 2020 at 15:48
  • Yes and this what confused me. Commented Mar 19, 2020 at 16:21

1 Answer 1

2

Try this and see what you get:

Sub Call_PowerShell_Script_File_From_Excel_VBA()
    'Windows PowerShell ISE >> New Script >> Create 'Hello World.ps1'
    '    echo "Hello World"
    '    $x = 1 + 1
    '    echo $x
    '----------------------------------------------------------------
    Dim wshShell        As Object
    Dim wshShellExec    As Object
    Dim strCommand      As String
    Dim strOutput

    strCommand = "Powershell.exe -File ""C:\Users\Future\Desktop\Hello World.ps1"""
    Set wshShell = CreateObject("WScript.Shell")
    Set wshShellExec = wshShell.Exec(strCommand)
    strOutput = wshShellExec.StdOut.ReadAll()
    Debug.Print "StdOut:", strOutput

    strOutput = wshShellExec.StdErr.ReadAll()
    Debug.Print "StdErr:", strOutput


End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot. I have put the result in the main post. How can I enable it on the system?
No idea - just pointing out the problem. If you're on a company PC then ask your IT folk. If you're at home then start here: google.com/…
Thank you very much for awesome help. It has been enabled by opening PowerShell as administrator then using Set-ExecutionPolicy Unrestricted then using this command too Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy ByPass -Force

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.