Even with PowerShell’s command-line argument “NoWindow”, a minimized powershell prompt is seen briefly by the user.
Using VBScripts ability to load console applications completely hidden, we can run PowerShell without interrupting the user.
The VBScript takes a single argument which is the filename of the powershell script. The VBScript needs to be in the same folder as the powershell script as it will work out its own scriptpath and use this to find the PowerShell script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Dim objShell,objFSO,objFile Set objShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objArgs = Wscript.Arguments ' Get the VBS script full path and extrapolate its parent directory strScriptPath = Wscript.ScriptFullName Set objScriptFile = objFSO.GetFile(strScriptPath) strFolder = objFSO.GetParentFolderName(objScriptFile) ' Take the string from arguments and prefix the parent directory strPath = strFolder & "\" & objArgs(0) 'verify file exists If objFSO.FileExists(strPath) Then 'return short path name set objFile=objFSO.GetFile(strPath) strCMD="powershell -nologo -executionpolicy bypass -command " & Chr(34) & "&{" & objFile.ShortPath & "}" & Chr(34) 'use 0 to hide window objShell.Run strCMD,0 Else 'Display error message WScript.Echo "Failed to find " & strPath WScript.Quit End If |