Home > Articles > Certification > Microsoft Certification

  • Print
  • + Share This
This chapter is from the book

Running Programs

Unlike cmd.exe, Exchange Management Shell (EMS) can process cmdlets in one of two ways. It analyzes what you have typed and decides whether it should be interpreted as an expression (Typing 1 + 1 produces a result of 2 in Expression mode) or as a command, which works similarly to the way that cmd.exe works. In Command mode, if what you have typed has no errors, it will be executed. If you begin a line of code with a number, a quote, or a dollar sign ($), EMS will function in Expression mode. This can be observed when you type $compute = 1+1 and press Enter. When you subsequently type $computer and press Enter, the output will be the number "2".

What if you are interested in running a program and you have defined the variable $Prog to run it—how can you tell EMS to execute $Prog as a command and not display it as a value? The use of the ampersand (&) character is how you can do this, as shown in the following table.

PS C:\UsersAdministrator>$Prog=
C:\Windows\System32\
calc.exe
PS C:\UsersAdministrator>$Prog
PS C:\UsersAdministrator>&$Prog

               

If you define the variable $Prog as shown in the example, the Windows Calculator will not launch when you subsequently type $Prog, but the result that is displayed will be C:\Windows\System32\calc.exe.

EMS is displaying the value of the variable, not executing it.

However, if you type &$Prog, the Windows Calculator will launch successfully.

To run a batch file, you need cmd.exe to launch first, so you could do something like what's shown in the following table.

PS C:\UsersAdministrator>cmd.exe /c
MyTest.bat
               

Here, /c tells the batch file to carry out the command and then terminate cmd.exe It is not necessary to use & with cmd.exe.

For a VBScript executable, you also need cmd.exe to launch first, so you could do something like what's shown in the following table.

PS C:\Users\Administrator>
&"cscript.exe MyVBScript.vbs
param1 param2"
               

Because you are launching another executable, & is required.

To run PowerShell scripts from a cmd.exe prompt, you need to use powershell.exe. In previous versions of PowerShell, you used msh.exe. Powershell.exe is run as shown in the following table.

PS C:\UsersAdministrator>
powershell.exe-noexit
"C:\MyScripts\MyTest.ps1"

               

Powershell.exe is the Windows PowerShell executable.

The -noexit switch is an optional parameter that tells the PowerShell console to remain open after the script finishes.

This technique will be illustrated in the section that follows.

  • + Share This
  • 🔖 Save To Your Account