0

I have a powershell script which I call using a .vbs file. This reduces the effort of the user to go to cmd every time,type powershell and then calling the .ps1 file. The vbs script directly opens powershell in a new cmd window calling the ps1 file.

The VB script is:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit C:\Scripts\anydrive.ps1")

What I want to achieve now is, Can I embed the .ps1 file inside .vbs file so that I have a single .vbs file which I can circulate to users rather than having 2 separate files.

9
  • By embedding I mean , can the entire code of .ps1 somehow be used in .vbs file itself or by any other logic which ultimately leaves me with one file only. Hope my query is clear. Commented Feb 25, 2015 at 17:04
  • You could probably save the contents as a string and send the string as an argument to the -command parameter. Question is though, why don't you just use one or the other...? Commented Feb 25, 2015 at 17:04
  • I was using only .ps1 file earlier but that made users in my office a bit annoying by everytime opening powershell explicitly. By giving them the .vbs file , they now only just need to double click on the .vbs file which calls the .ps1 file in the backend and provides them .ps1 menu. Commented Feb 25, 2015 at 17:09
  • I see. Whatever it is that you're doing in powershell you can almost certainly also do natively in vbscript so you may be overcomplicating matters here. Commented Feb 25, 2015 at 17:10
  • 1
    Google is your friend. Yes you can call WMI from VBS Commented Feb 25, 2015 at 17:24

1 Answer 1

3

Yes

You can do what you're asking. The easiest way to is to encode it as a base64 string, perhaps something like this (this is a one time thing):

$script = Get-Content C:\Scripts\anydrive.ps1 -Raw
$bytes = [System.Text.Encoding]::Unicode.GetBytes($script)
$encoded = [Convert]::ToBase64String($bytes)
$encoded | clip  # copy it to the clipboard

Then you paste it into the VBScript encoded.

You would call powershell like this:

powershell.exe -NoExit -EncodedCommand $e

($e in this case refers to the encoded command string or the VBscript variable that contains it).

But, why?

If you don't want users to have to open powershell and manually load the file, and you want something they can double click on, why not just make a shortcut for them? The shortcut would just target the exact same command line you're calling from the VBscript.

Sign up to request clarification or add additional context in comments.

2 Comments

"But, why?" - common sense prevails :)
Thanks Braintist and everyone for the replies.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.