Run Programs, Scripts, and Existing Tools

1.2.1. Problem

You rely on a lot of effort invested in your current tools. You have traditional executables, Perl scripts, VBScript, and of course, a legacy build system that has organically grown into a tangled mess of batch files. You want to use PowerShell, but don’t want to give up everything you already have.

1.2.2. Solution

To run a program, script, batch file, or other executable command in the system’s path, enter its filename. For these executable types, the extension is optional:

	Program.exe arguments
	ScriptName.ps1 arguments
	BatchFile.cmd arguments

 

To run a command that contains a space in its name, enclose its filename in single-quotes (‘) and precede the command with an ampersand (&), known in PowerShell as the Invoke operator:

	& 'C:\Program Files\Program\Program.exe' arguments

 

To run a command in the current directory, place .\ in front of its filename:

	.\Program.exe arguments

 

To run a command with spaces in its name from the current directory, precede it with both an ampersand and .\:

	& '.\Program With Spaces.exe' arguments

1.2.3. Discussion

In this case, the solution is mainly to use your current tools as you always have. The only difference is that you run them in the PowerShell interactive shell, rather than cmd.exe.

The final three tips in the solution merit special attention. They are the features of PowerShell that many new users stumble on when it comes to running programs. The first is running commands that contain spaces. In cmd.exe, the way to run a command that contains spaces is to surround it with quotes:

	"C:\Program Files\Program\Program.exe"

 

In PowerShell, though, placing text inside quotes is part of a feature that lets you evaluate complex expressions at the prompt, as shown in Example 1-1.

Example 1-1. Evaluating expressions at the PowerShell prompt

 

PS >1 + 1
2
PS >26 * 1.15
29.9
PS >"Hello" + " World"
Hello World
PS >"Hello World"
Hello World
PS >"C:\Program Files\Program\Program.exe"
C:\Program Files\Program\Program.exe
PS >

So, a program name in quotes is no different from any other string in quotes. It’s just an expression. As shown previously, the way to run a command in a string is to precede that string with the invoke (&) operator. If the command you want to run is a batch file that modifies its environment, see Section 1.8, “Program: Retain Changes to Environment Variables Set by a Batch File.”

 

By default, PowerShell’s security policies prevent scripts from running. Once you begin writing or using scripts, though, you should configure this policy to something less restrictive. For information on how to configure your execution policy, see Section 16.1, “Enable Scripting Through an Execution Policy.”

 

The second command that new users (and seasoned veterans before coffee!) some-times stumble on is running commands from the current directory. In cmd.exe, the current directory is considered part of the path—the list of directories that Windows searches to find the programname you typed. If you are in the C:\Programs directory, cmd.exe looks in C:\Programs (among other places) for applications to run.

PowerShell, like most Unix shells, requires that you explicitly state your desire to run a program from the current directory. To do that, you use the .\Program.exe syntax, as shown previously. This prevents malicious users on your system from littering your hard drive with evil programs that have names similar to (or the same as) commands you might run while visiting that directory.

To save themselves from having to type the location of commonly used scripts and programs, many users put these utilities along with their PowerShell scripts in a “tools” directory, which they add to their system’s path. If PowerShell can find a script or utility in your system’s path, you do not need to explicitly specify its location.

 

Scripts and examples from this book are available at http://www.oreilly.com/catalog/9780596528492.

 

To learn how to write a PowerShell script, see Section 10.1, “Write a Script.

1.2.4. See Also

  • Section 1.8, “Program: Retain Changes to Environment Variables Set by a Batch File”

  • Section 10.1, “Write a Script

  • Section 16.1, “Enable Scripting Through an Execution Policy”

Tags: , , , , ,