Converting VBScript's Execute Statement

Windows PowerShell

Definition: Executes one or more specified statements.


The VBScript Execute statement enables you to assign an expression (typically lines of VBScript code) to a variable; you can then “execute” that variable as though it was code hard-coded into the script itself (as opposed to code existing only in memory).

Don’t worry about it; we’ll just show you what we mean. In the following example, the string value “get-date” is assigned to the variable $a. The Invoke-Expression Cmdlet is then used to execute $a as though the value had been typed from the command prompt:

$a = "get-date"
invoke-expression $a

When you run these two commands you’ll get back the current date and time (because the string value "get-date" will be executed as a command).

Return to the VBScript to Windows PowerShell home page