Introduction to Windows PowerShell

1 2 3 4 5 6 7 8 9 10 11 12 Page 6
Page 6 of 12

Pausing Output and Stopping a Runaway Program

As in the regular Command Prompt environment, you can type Ctrl+C to cancel a running PowerShell program or script.

In addition, Ctrl+S stops a printout that’s scrolling by so fast you can’t read it, and another Ctrl+S starts things up again. You can also use the scrollbar to look back through the contents of the PowerShell window.

Still, if a command is going to generate a lot of output, just as in the Command Prompt environment, it’s best to either pipe the output through the more command, which pauses after every screenful, or direct the output into a file, and then view the file with Notepad or a word processor.

Command-Line Syntax

PowerShell’s command-line syntax is at once familiar and a bit strange. I discuss this in the following sections.

Command Types

As I mentioned previously, PowerShell has built-in language statements, and it can run internal cmdlets, external commands, and scripts that contain multiple PowerShell commands.

With the exception of the PowerShell language keywords like if and foreach, which can have complex structures, commands are evaluated one line at a time, just as in the Command Prompt world.

The first word on the line is the name of the command you want to run. Any remaining text on the line is passed to the command as its arguments. Normally, spaces delimit the command name and the arguments, but you can use the single or double quotation mark () character to embed a space in an argument, as in the following command:

command /abc “argument with spaces”

In an unusual twist, to type a program name that has spaces in its name or path, you must use single or double quotes around the name and must precede the command with an ampersand, as in this example:

&"command with spaces" /abc argument

PowerShell finds the program that corresponds to the command name by searching the following places in this order:

  1. PowerShell first looks through its alias list, which is a list of shorthand names for commands. PowerShell is installed with nearly 150 built-in aliases predefined. For example, cd is set up as the alias for the Set-Location cmdlet. As you work with PowerShell, you’ll find that you’ll soon want to define your own custom aliases for the commands you use most often.
  2. If an alias name is matched, PowerShell replaces the command name in your command line with the alias definition and the search process over.
  3. PowerShell then scans the names of functions that have been defined by the current script, if you’re running a PowerShell script, or that have been defined at the command line. If a function with the specified name is found, the function is called and passed the command-line arguments. You can define your functions in the PowerShell language in scripts or at the command prompt. They work and act just like cmdlets.
  4. If the command name is not found in the alias list, PowerShell searches the list of cmdlets that come preinstalled with Windows PowerShell or have been added by a snap-in that came with an added application or service. If it’s found, the cmdlet is run.
  5. If the name doesn’t match a cmdlet, PowerShell uses the standard search path mechanism to look for an external program, much as cmd.exe does. It looks first in the current directory and then through the directories listed in the PATH environment variable for a file with the stated command name and whose extension is a recognized program extension. In each folder searched, PowerShell does the following:
    • It first looks for the extension .ps1, which is used for PowerShell scripts in PowerShell versions 1.0 and 2.0. If a script file is found, PowerShell runs the script (subject to the security restrictions that I discuss shortly).
    • If a .ps1 file is not found, it scans through the extensions listed in the PATHEXT environment variable, just as cmd.exe would. This mechanism lets it find and run standard Windows executable files (.exe files), both GUI and console applications. PATHEXT also includes .cmd and .bat, so that PowerShell can run normal Command Prompt batch files—it fires up a copy of cmd.exe to do so. Likewise, for .vbs, .js, .vbe, and .jse files, it fires up WSH to run these scripts, and for .MSC files, it runs the snap-in through the Microsoft Management Console.
  6. Thus, you can run PowerShell cmdlets and scripts, console applications, Windows programs, batch files, or any other sort of program from the PowerShell environment. notepad still starts Notepad.

1 2 3 4 5 6 7 8 9 10 11 12 Page 6
Page 6 of 12
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon