Skip to content

Instantly share code, notes, and snippets.

@SVNKoch
Created November 2, 2024 01:18
Show Gist options
  • Select an option

  • Save SVNKoch/93e272ccaff1bff19f429d7bf3c51725 to your computer and use it in GitHub Desktop.

Select an option

Save SVNKoch/93e272ccaff1bff19f429d7bf3c51725 to your computer and use it in GitHub Desktop.
execute any ps1 cmd bat or vbs script without a command line interface popping up
' check if script file argument is present
If WScript.Arguments.Count = 0 Then
WScript.Echo "Please provide a script file as an argument."
WScript.Quit 1
End If
scriptFile = WScript.Arguments(0)
' collect any additional arguments
args = ""
If WScript.Arguments.Count > 1 Then
For i = 1 To WScript.Arguments.Count - 1
args = args & " """ & WScript.Arguments(i) & """"
Next
End If
' resolve relative paths and check file presence
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(scriptFile) Then
If Not fso.FolderExists(fso.GetParentFolderName(scriptFile)) Then
scriptFile = fso.BuildPath(fso.GetParentFolderName(WScript.ScriptFullName), scriptFile)
End If
If Not fso.FileExists(scriptFile) Then
WScript.Echo "The specified script file does not exist: " & scriptFile
WScript.Quit 1
End If
End If
fileExtension = LCase(fso.GetExtensionName(scriptFile))
Set objShell = CreateObject("WScript.Shell")
Select Case fileExtension
Case "bat", "cmd"
objShell.Run "cmd /c " & Chr(34) & scriptFile & Chr(34) & args, 0, True
Case "ps1"
objShell.Run "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File " & Chr(34) & scriptFile & Chr(34) & args, 0, True
Case "vbs"
objShell.Run "wscript.exe " & Chr(34) & scriptFile & Chr(34) & args, 0, True
Case Else
WScript.Echo "Unsupported file type: " & fileExtension
WScript.Quit 1
End Select
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment