首页 > 解决方案 > 执行脚本时隐藏powershell窗口

问题描述

我是使用 Powershell 和脚本的新手。在脚本文件中有一些我想运行的简单命令,一切正常

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

我的问题是,运行代码时,powershell窗口出现两秒钟。如何隐藏它以便根本不显示此窗口?

标签: excelvbapowershell

解决方案


您将在命令中添加一个附加参数来调用 PowerShell 以保持窗口隐藏。调整strCommand到这样的东西:

strCommand = "Powershell -WindowStyle Hidden -File ""C:\Users\Future\Desktop\Test.ps1"""

推荐阅读