6

Hey i want to run from an vbs script a powershell commando. Something like start powershell.exe and enter a specific command like Restart-Service. I thought something similar to this could work:

strCommand = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command Restart-Service [service name]"

    Set WshShell = WScript.CreateObject("WScript.Shell") 
    Set objExec = WshShell.Exec(strCommand)

Has someone an idea how can i manage this?

4
  • 1
    This will help: ss64.com/vb/run.html Commented Jul 12, 2012 at 9:13
  • 1
    i have set it up like this Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") objShell.run "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe & Restart-Service BranchCache" Set objShell = Nothing But is doesn't work. Commented Jul 12, 2012 at 9:28
  • 1
    Look at PowerShell.exe's command line parameters. Use the one to tell PowerShell to run a command... Commented Jul 12, 2012 at 9:42
  • 1
    Ok i have looke now for the parameter and setuped it like this: Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") objShell.run "PowerShell -Command {Start-Service PeerDistSvc}" When i run the vbs it runs through and the cmd window pops up and close and that was it, but the service didn't start what do i wrong? I am administrator on the computer. Commented Jul 12, 2012 at 10:57

6 Answers 6

6

1) store your powershell command as powershell script.
2) Use the vbs to run the powershell

  Set objShell = CreateObject("Wscript.Shell")
 objShell.Run("powershell.exe -noexit c:\scripts\test.ps1")
Sign up to request clarification or add additional context in comments.

3 Comments

This is the last thing i try when i can't get it working i use youre method.
@HariHaran Hello do you know why this might work when a call via objShell.Run "powershell -file ""c:\scripts\test.ps1""", 0, true doesn't?
Is there a reason why C:\Users\lpeder6\Desktop\PowerShell Scripts\GUI Files\Basic GUI Script - Combo Box.ps1 opens in Windows PowerShell ISE, but not when I call it from a VBScript (example: objShell.Run ("powershell.exe -noexit C:\Users\lpeder6\Desktop\PowerShell Scripts\GUI Files\Basic GUI Script - Combo Box.ps1"))?
3

Try this:

powershell -command '& {command to run}'

/G

3 Comments

I think it works ,but i noticed i need to be administrator to start/stop services how can i run this part of the vbs as a different user?
I'm not quite sure. You could try something like this: stackoverflow.com/questions/1566969/…
and I think there is also a switch (-verb runas). Try googling it
1

You can use the shortest version.

CreateObject("Wscript.Shell").Run ("powershell.exe -noexit $c= 'lola'; write-host $c -ForegroundColor green")

Comments

0

Try this:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit .\c:\scripts\test.ps1")

Or keep the files in the same folder where the PS exe is and then

Objshell.Run("powershell.exe -noexit .\test.ps1")

1 Comment

or u can use objShell.Run("powershell.exe -noexit -file c:\scripts\test.ps1")
0

While researching how to use this to run PowerShell scripts using VBScript, I was running into the issue shown below:

enter image description here

I've since then figured out why it was not working. PowerShell cannot read spaces in the location of your script. As you can see in the image above, the error stops at C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays when the full path is C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays - Clear Method.ps1.

After removing all spaces and extra characters from my path names, running PowerShell scripts with VBScript works with the code below:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit -command C:\Users\lpeder6\Desktop\PowerShellScripts\GUIFiles\BasicGUIScript-ComboBox.ps1"),1,True

What I get, when I run it, is this:

enter image description here

I hope this helps someone as this is information I really could have used. :)

2 Comments

No need to change the path, what if you can't change the path and it has spaces? See Spaces cause split in path with PowerShell
@user692942 That worked! I've never heard of anything like that and it has caused a lot of issues during development of other solutions we've been looking into. This will really help us move forward using PowerShell.
0

Thank you , it worked for me after I removed the .\ from

objShell.Run("powershell.exe -noexit .\C:\Users\503327978\Downloads\Scripts\test\ONLY_popups_messages.ps1")

to

objShell.Run("powershell.exe -noexit C:\Users\503327978\Downloads\Scripts\test\ONLY_popups_messages.ps1")

Therefore my entire script is as followed:

Set objShell = CreateObject("Wscript.Shell") objShell.Run("powershell.exe -noexit C:\Users\503327978\Downloads\Scripts\test\ONLY_popups_messages.ps1")

worked perfectly, plus is shorter version. :-)

Comments

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.