2

I am newbie at using Powershell and scripts .. In script file there are simple commands that I would like to run and everything is ok

Sub TestUDF()
MsgBox ScriptFile
End Sub

Function ScriptFile() As String
Dim wshShell        As Object
Dim wshShellExec    As Object
Dim strCommand      As String
Dim strOutput       As String

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

ScriptFile = strOutput
End Function

My problem is that when running the code, the powershell window appears for two seconds. How can I hide it so as not to display this window at all?

1 Answer 1

3

You would add an additional parameter on your command to call PowerShell to keep the window hidden. Adjust the strCommand to something like this:

strCommand = "Powershell -WindowStyle Hidden -File ""C:\Users\Future\Desktop\Test.ps1"""
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I tried this parameter -WindowStyle Hidden and also -nologo but the window appears for a second ...
I would say then it is something Excel is overwriting the normal behavior. Nothing you can do about that, that I'm aware of.

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.