Treps
Treps

Reputation: 800

Run PowerShell script from folder Program Files via VBScript

How can I run a PowerShell script from within the %ProgramFiles% folder?

I have tried the variable %ProgramFiles% without any progress, also "Program Files" but can't get ut to work.

My current code

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell C:\Program Files\Temp\test.ps1"), 0 , True

It's working if I use a network share without any spaces

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell \\domain\SYSVOL\script\test.ps1"), 0 , True

I have also tried using """ but can't get PowerShell to work.

Anyone know how to solve this? I need to use VBScript in order to start the PowerShell script in the background at logon via a GPO. Otherwise it will show a PowerShell window.

Upvotes: 0

Views: 509

Answers (1)

Drasen
Drasen

Reputation: 61

Try this. Add parameter "-file" and double quotes around the path. You can get and store %ProgramFiles% in a variable using ExpandEnvironmentStrings:

Set shell = WScript.CreateObject("WScript.shell")

sProgramFiles = shell.ExpandEnvironmentStrings("%PROGRAMFILES%")

shell.Run "powershell -file """ & sProgramFiles & "\Temp\test.ps1""", 0 , True

Upvotes: 1

Related Questions