Most old VBS scripts can be easily translated to PowerShell. The key command in VBS is “CreateObject” which lets you access system libraries. PowerShell translates “CreateObject” to “New-Object -ComObject”, yet the object model and member names stay the same:

This VBS script talks to you when you save it to a text file with “.vbs” extension:

Set obj = CreateObject("Sapi.SpVoice")

obj.Speak "Hello World!"

The PowerShell equivalent looks like this:

$obj = New-Object -ComObject "Sapi.SpVoice"
$obj.Speak("Hello World!")

There are only a few built-in VBS members such as MsgBox or InputBox. To translate these, all you need is importing the “Microsoft.VisualBasic.Interaction” type. This would be the PowerShell code to invoke MsgBox or InputBox:

Add-Type -AssemblyName Microsoft.VisualBasic

$result = [Microsoft.VisualBasic.Interaction]::MsgBox("Do you want to restart?", 3, "Question")
$result

$result = [Microsoft.VisualBasic.Interaction]::InputBox("Your name?", $env:username, "Enter Name")

Here is the complete list of supported Visual Basic members:

 
PS> [Microsoft.VisualBasic.Interaction] | Get-Member -Stati


   TypeName: Microsoft.VisualBasic.Interaction

Name            MemberType Definition                                          
----            ---------- ----------                                          
AppActivate     Method     static void AppActivate(int ProcessId), static vo...
Beep            Method     static void Beep()                                  
CallByName      Method     static System.Object CallByName(System.Object Obj...
Choose          Method     static System.Object Choose(double Index, Params ...
Command         Method     static string Command()                             
CreateObject    Method     static System.Object CreateObject(string ProgId, ...
DeleteSetting   Method     static void DeleteSetting(string AppName, string ...
Environ         Method     static string Environ(int Expression), static str...
Equals          Method     static bool Equals(System.Object objA, System.Obj...
GetAllSettings  Method     static string[,] GetAllSettings(string AppName, s...
GetObject       Method     static System.Object GetObject(string PathName, s...
GetSetting      Method     static string GetSetting(string AppName, string S...
IIf             Method     static System.Object IIf(bool Expression, System....
InputBox        Method     static string InputBox(string Prompt, string Titl...
MsgBox          Method     static Microsoft.VisualBasic.MsgBoxResult MsgBox(...
Partition       Method     static string Partition(long Number, long Start, ...
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, S...
SaveSetting     Method     static void SaveSetting(string AppName, string Se...
Shell           Method     static int Shell(string PathName, Microsoft.Visua...
Switch          Method     static System.Object Switch(Params System.Object[...



PS>  
 

Twitter This Tip! ReTweet this Tip!

Anonymous