How do I make Powershell run VBS files in the console without affecting double click behaviour?
On my PC, VBS files are set (via the file association entries in the registry) to run using WScript. This is what I want, so that I can double click on a VBS file and not have a console window open. However, when I am using the Powershell command line, I would like VBS scripts to run using CScript (i.e., using "cscript /nologo myfile.vbs"). Is there a way to make Powershell do this, without affecting the double-click behaviour?
For single scripts, I could write a function for each one (function foo { cscript /nologo foo.vbs $args }) but that doesn't scale at all. I开发者_运维技巧'm thinking of something like Take Command's "executable extensions" feature, that lets you set a specific action for a file extension (set .vbs=cscript /nologo).
I have the same problem for Python scripts, where again the default action doesn't do what I want (for some reason, it opens a second console window, rather than displaying output in the Powershell console) so a VBS-specific answer will only give me part of what I want :-(
Thanks, Paul
The easiest thing i can think of is, to set an environment variable whenever you start Power Shell (does it have an autoexec feature?) let's call it "InPowerShell" and give it the value "YES".
Then create a .bat file like this:
if %InPowerShell%==YES (
cscript /nologo #*
) else (
wscript #*
)
Set all vbs files to open with this bat file. This way whenever you double click it it will be opened with wscript, and inside powershell will open with cscript.
(Note: I didn't test this)
精彩评论