Dec
4
2012

How to execute VBScript through a PowerShell Script

I was eagerly searching for a solution to call a VBScript through PowerShell script, after going through several articles and collecting information in bits and pieces from various sources written a very small and useful code, you can use it at your wish,

My requirement: Call vbscript along with passing two arguments to it, one is server name and other one is a folder name.

 

Command:

1. Declare $args to collect arguments (in this case server name and folder name I collected in two variables from my other commands in PowerShell script

$args = “$Server $FolderName”

 

2. Create a command statement basically calling cmd to call cscript and execute test.vbs which resides in local folder, you can specify full path to vbs if required (don’t foget to include double quotes)

$command = “cmd /C cscript .\test.vbs $args”

 

3. Now use invoke-expression method to execute it

invoke-expression $command

 

Note: if you wish to execute vbs to remote computer, then you use invoke-command method but you need to test it first; as my requirement was different hence not tested it, but it should work for simple vbs.

 

My final script looks like,

 

$args = “$Server $FolderName”

$command = “cmd /C cscript .\test.vbs $args”

invoke-expression $command

 

If you are new to programming and struggled for this one, then hope you find it useful :)

Download PDF

About the Author: Nitin G

Indian born, trekker, biker, photographer, lover of monsoons. I've been working full time with SQL Server since year 2005 and blogs to post the content aquired during my research on new topics or fixing issues faced by me as a DBA while working in different kind of projects, hope some of my posts may helps others in SQLDBA community. Everything you read on my blog is my own personal opinion and any code is provided "AS-IS" with no warranties!

4 Comments + Add Comment

  • Hi, I am to do the following:

    $source= “http://…/test.vbs”
    $destination= “c:\test.vbs”
    $wc= New-Object System.Net.WebClient
    $wc.DownloadFIle($source, $destination)
    $command= “cmd /C C:/test.vbs”
    Invoke-Expression $command

    test.vbs gets downoaded in C:/ . But nothing happens after that. The vb script is not running. The code hangs at this step. Is there something wrong that I am doing?
    Thanks in advance.

    • Replying after an year :( but yes, if you still didn’t get this resolved then it generally linked to the OS privileges, if you download an online script then you may need to unblock them or run with admin privileges to workout. I would say test it with downloaded script first and if it works then issue is 99% with privileges. hope this helps.

  • you are a godsend. Thanks for this one

    • Thanks Shanay! :)

Leave a comment

Subscribe to this blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 446 other subscribers

Translate this blog!

EnglishFrenchGermanItalianPortugueseRussianSpanish

Calender

June 2017
M T W T F S S
« May    
 1234
567891011
12131415161718
19202122232425
2627282930  

View Post by Categories

%d bloggers like this: