Windows PowerShell Parse Methods

10/20/2009

Windows PowerShell parses in two modes—command mode and expression mode. In expression mode, Windows PowerShell parses as most high-level languages parse: numbers are numbers; strings need to be quoted, and so on. Expressions are things such as:

2+2

4

"Hello" + " world"

Hello world

$a = "hi"

$a.length * 13

26

When parsing in command mode, strings do not need to be quoted and everything is treated like a string except variables and things in parentheses. For example:

copy users.txt accounts.txt

users.txt and accounts.txt are treated as strings

write-host 2+2

2+2 is treated as string, not an expression to evaluate

copy $src $dest

$src and $dest are variables. Not needing to use quotation marks when working in a command shell can be very beneficial in the long run because it greatly reduces the amount of typing required.

The parsing mode is determined by the first token encountered. If the token is a number, variable, or quoted string, then the shell parses in expression mode. If the line starts with a letter, an & (ampersand), or a . (dot) followed by a space or a letter, the parsing is done in command mode.

2+2

Expression mode starts with a number.

"text"

Expression mode starts with a quotation mark.

text

Command mode starts with a letter.

& "text"

Command mode starts with an ampersand.

. "file.ps1"

Command mode starts with a dot followed by a space.

.125

Expression mode starts with a dot, which is followed by a number—not a space or a letter.

.text

Command mode starts with a dot, which is part of the command name ".text"

It is very useful to be able to mix expressions and commands; which you can do by using parentheses. Inside parentheses, the mode discovery process starts over.

Write-Host (2+2)

2+2 is treated as an expression to evaluate and is passed to the Write-Host command.

(Get-Date).day + 2

Get-Date is treated as a command, and the result of executing it becomes the left value in the expression.

You can nest commands and expressions without restrictions.

Write-Host ((Get-Date).day + 2)

Get-Date is a command. ((Get-Date).day+2) is an expression, and Write-Host ((Get-Date).day + 2) is a command again.

Write-Host ((Get-Date) - (Get-Date).date)

The Get-Date command is used twice to determine how much time has passed since midnight (condition).

Posted in: .NET Framework MS Windows| Tags: Windows PowerShell Command Windows PowerShell parse expression mode numbers strings quoted Get-Date Write-Host condition

Understanding Important Windows PowerShell Concepts

07/25/2009

The Windows PowerShell design integrates concepts from many different environments. Several of them are familiar to people with experience in specific shells or programming environments, but very few people will know about all of them. Looking at some of these concepts provides a useful overview of the shell.

Commands are not Text-based

Unlike traditional command-line interface commands, Windows PowerShell cmdlets are designed to deal with objects - structured information that is more than just a string of characters appearing on the screen. Command output always carries along extra information that you can use if you need it. We will discuss this topic in depth in this document.

If you have used text-processing tools to process command-line data in the past, you will find that they behave differently if you try to use them in Windows PowerShell. In most cases, you do not need text-processing tools to extract specific information. You can access portions of the data directly by using standard Windows PowerShell object manipulation commands.

The Command Family is Extensible

Interfaces such as Cmd.exe do not provide a way for you to directly extend the built-in command set. You can create external command-line tools that run in Cmd.exe, but these external tools do not have services, such as help integration, and Cmd.exe does not automatically know that they are valid commands.

The native binary commands in Windows PowerShell, known as cmdlets (pronounced command-lets), can be augmented by cmdlets that you create and that you add to Windows PowerShell by using snap-ins. Windows PowerShell snap-ins are compiled, just like binary tools in any other interface. You can use them to add Windows PowerShell providers to the shell, as well as new cmdlets.

Because of the special nature of the Windows PowerShell internal commands, we will refer to them as cmdlets.

Note:

Windows PowerShell can run commands other than cmdlets. We will not be discussing them in detail in the Windows PowerShell Primer, but they are useful to know about as categories of command types. Windows PowerShell supports scripts that are analogous to UNIX shell scripts and Cmd.exe batch files, but have a .ps1 file name extension. Windows PowerShell also allows you to create internal functions that can be used directly in the interface or in scripts.

Posted in: .NET Framework Software Programming| Tags: PowerShell Concept Command Text-based Extensible Integrate Environment Familiar Cmdlets PS

Hot Posts

Latest posts

Tags

Others

Sponsors