How to check default host for VBScript is WScript or CScript?
Asked Answered
E

4

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" %*
Equitation answered 3/2, 2016 at 20:4 Comment(3)
To see the executable command linked to VBScript files, you can check output of ftype VBSFile command.Graber
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' sectionEquitation
Ditto--ftype doesn't seem to work for this on Windows 7 and always returns the command for WScript.exe.Budge
B
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
Brooch answered 3/2, 2016 at 22:39 Comment(1)
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 ?Equitation
T
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
Therese answered 25/3, 2023 at 12:23 Comment(0)
C
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.

Cheatham answered 12/4, 2023 at 8:5 Comment(0)
M
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
Mop answered 19/4, 2023 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.