3

I would like to know what is default host for VBScript on particular machine, whether that is set to WScript or CScript ? For example, if I use cscript //h:cscript //s then is there any way I can check host for VBScript is set to cscript?

I found commands to change default host but did not find command to check default host.

Edit:

C:\Windows\system32>cscript //h:cscript //s

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Command line options are saved.
The default script host is now set to cscript.exe.

C:\Windows\system32>ftype VBSFile
VBSFile="%SystemRoot%\System32\WScript.exe" "%1" %*
zx485
  • 28,498
  • 28
  • 50
  • 59
Magg
  • 103
  • 2
  • 7
  • To see the executable command linked to VBScript files, you can check output of `ftype VBSFile` command. – Kul-Tigin Feb 03 '16 at 20:28
  • I changed default host to cscript and used "ftype VBSFile" but I did not see cscript.exe as output of "ftype VBSFile".. See original post 'Edit' section – Magg Feb 03 '16 at 21:01
  • 1
    Ditto--ftype doesn't seem to work for this on Windows 7 and always returns the command for WScript.exe. – Tony Hinkle Feb 03 '16 at 21:43

4 Answers4

0

How Can I Determine the Default Script Host on a Computer Before I Run a Script?

Const HKEY_CLASSES_ROOT = &H80000000
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "VBSFile\Shell\Open\Command"
objRegistry.GetExpandedStringValue HKEY_CLASSES_ROOT,strKeyPath,vbNullString,strValue
strValue = LCase(strValue)
Wscript.Echo strValue
If InStr(strValue, "wscript.exe") then
    Wscript.Echo "WScript"
Else
    Wscript.Echo "CScript"
End If
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • I checked manually in registry but it shows value "%SystemRoot%\System32\WScript.exe" "%1" %*..... But I set it to run as cscript.. So it should show value as cscript.exe ? – Magg Feb 08 '16 at 20:01
0

You can find the current default host by saving the below code as a batch file:

@echo off
For /f "Tokens=3" %%s in ('Reg Query "HKCR\VBSFile\Shell" /ve') Do Set "Shell=%%s"
For /f "Tokens=3" %%c in ('Reg Query "HKCR\VBSFile\Shell\%Shell%\Command" /ve') Do Set "Command=%%c"
For /f Tokens^=4^ Delims^=\^" %%s in ('echo %Command%') Do echo %%s

Or you can write it as one-liner:

@For /f "Tokens=3" %%s in ('Reg Query "HKCR\VBSFile\Shell" /ve') Do For /f "Tokens=3" %%c in ('Reg Query "HKCR\VBSFile\Shell\%%s\Command" /ve') Do For /f Tokens^=4^ Delims^=\^" %%s in ('echo %%c') Do echo %%s
0

OP's edit is the most concise way but if you must query the registry for whatever reason, the proper way is by looking at the file handler in the registry. PowerShell example below

(Get-ItemProperty registry::HKEY_CLASSES_ROOT\VBSFile\Shell).'(Default)'

Where "Open" is WScript and "Open2" is CScript.

0

I never check it...I make all my scripts for CScript and use this Print sub for all output to console. It gives an error if the Host is Wscript. Just print a welcome message before doing anything. A MsgBox wrapper could be done for the contrary case...

  Sub print(s): 
    On Error Resume Next
    WScript.stdout.WriteLine (s)  
    If  err= &h80070006& Then WScript.Echo " Please run this script with CScript": WScript.quit
  End Sub
Antoni Gual Via
  • 714
  • 1
  • 6
  • 14