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 🙂

4 Comments + Add Comment
Leave a comment
Subscribe to this blog via Email
Old Posts
- November 2017 (3)
- October 2017 (4)
- September 2017 (2)
- May 2017 (1)
- April 2017 (1)
- July 2016 (3)
- May 2016 (1)
- April 2016 (1)
- February 2016 (2)
- January 2016 (1)
- October 2015 (1)
- September 2015 (1)
- August 2015 (1)
- July 2015 (2)
- June 2015 (3)
- April 2015 (1)
- March 2015 (1)
- December 2014 (1)
- September 2014 (2)
- April 2014 (1)
- January 2014 (3)
- October 2013 (2)
- September 2013 (2)
- August 2013 (4)
- July 2013 (1)
- June 2013 (2)
- May 2013 (5)
- April 2013 (3)
- March 2013 (1)
- February 2013 (9)
- January 2013 (11)
- December 2012 (14)
- November 2012 (3)
- October 2012 (4)
- July 2012 (2)
- June 2012 (3)
- May 2012 (2)
- April 2012 (8)
- March 2012 (6)
- February 2012 (3)
- January 2012 (1)
- December 2011 (5)
- November 2011 (8)
- October 2011 (5)
- September 2011 (3)
- August 2011 (3)
- July 2011 (3)
- May 2011 (1)
- November 2010 (1)
Tags
Calender
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
« Nov | ||||||
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 | 31 |
View Post by Categories
Recent Articles
- Setting up Always ON Availability Group in Multi Subnet Cluster – Recommendations
- Configuring Replication with Always ON Availability Group
- Login failed for user ‘DOMAIN\COMPUTER$’. Reason: Could not find a login matching the name provided. [CLIENT: ]
- Modern Servicing Model (Service Pack and Cumulative Updates) for SQL Server 2017 and onwards
- Fix: SSMS 2012 opening Debug window when pressing F5
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! 🙂