2

I'd like to know whether it is at all possible to pass a script as a string to cscript/wscript, similar to the -Command option of PowerShell. Alternatively, is there another way to execute VBS scripts from cmd?

The documentation of cscript/wscript does not list such an option, but as I'm not very familiar with Windows scripting, I am wondering whether I am missing something.

Thanks!

3
  • cscript /? returns Usage: CScript scriptname.extension [option...] [arguments...]. Esoteric? Read Run a VBScript / Windows Scripting Host (WSH) script. Commented Apr 2, 2015 at 15:53
  • 3
    No this is a common feature of modern scripting languages, e.g. the CPython interpreter provides the -c "pass script as string" option. (For powershell see answer below). It is quite useful for environments were you want to control another interpreter but can not create files. Commented Apr 7, 2015 at 12:36
  • It is very beneficial to pass a small script without having to access the file system first. For example, the Python interpreter supports it with -c. This works: python -c "print 3+4" Commented Feb 21, 2017 at 15:57

2 Answers 2

2

One way to do this is to write a vbs file from cmd via echo >, then execute it, then delete it.

Example:

echo WScript.Echo WScript.CreateObject("WScript.Shell").SpecialFolders(WScript.Arguments(0)) > test.vbs
cscript test.vbs "Desktop" //nologo
del test.vbs
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best answer and can be used to create a shortcut that runs an executable hidden: cmd.exe /c echo CreateObject("Wscript.Shell").Run "notepad.exe", 0, false > run_hidden.vbs & start /wait wscript run_hidden.vbs & del run_hidden.vbs
1

Many (scripting) languages have a R(ead)-E(valuate)-P(rint)-L(oop) - Tool and/or process strings given on the command line. So using Powershell you can do:

powershell -command (2+3)*10
50

Perl:

perl -e "print 'hello';"
hello

For VBScript you have to roll your own; maybe starting starting here or here(ff).

A very simple (proof of concept) script that just deals with code from the command line:

Option Explicit

Dim goWAN : Set goWAN = WScript.Arguments.Named
Dim goWAU : Set goWAU = WScript.Arguments.UnNamed

If goWAN.Count <> 1 Or goWAU.Count <> 1 Or Not (goWAN.Exists("x") Or goWAN.Exists("e")) Then
   WScript.Echo "usage: ", WScript.ScriptName, "/x|/e ""code to execute/evaluate"" (use ' for "")"
Else
   Dim sCode : sCode = Replace(goWAU(0), "'", """")
   If goWAN.Exists("x") Then
      Execute sCode
   Else
      WScript.Echo Eval(sCode)
   End If
End If

output:

cscript 29416456.vbs
usage:  29416456.vbs /x|/e "code to execute/evaluate" (use ' for ")

cscript 29416456.vbs /e "(2+3)*10"
50

cscript 29416456.vbs /x "WScript.Echo 'Now: ' & Now"
Now: 4/3/2015 10:53:49 PM

2 Comments

What is the significance of the filename 29416456? - Thx
It is the StackOverflow question Id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.