• No results found

Variable Related Cmdlets

To work with variables using the variable provider, the following Cmdlets are built in Windows PowerShell.

Windows PowerShell executes the command depending on the command input. If a native Windows command was typed, PowerShell will execute the command in a way similar to how Windows would execute in a CMD command shell.

Similarly, if you write a mathematical or a comparison expression in the PowerShell console, it will process accordingly.

1. Type the following command in the PowerShell console:

PS C:\> ipconfig.exe

Wireless LAN adapter Wireless Network Connection:

Media State . . . : Media disconnected Connection-specific DNS Suffix . :contoso.com

Ethernet adapter Local Area Connection:

Media State . . . : Media disconnected Connection-specific DNS Suffix . :contoso.com

Tunnel adapter isatap.corp.microsoft.com:

Media State . . . : Media disconnected Connection-specific DNS Suffix . :

This behavior of PowerShell is also known as Argument mode processing.

2. Using a PowerShell cmdlet, we can see similar behavior with arguments by running the following command in the console:

PS C:\> Write-Host 6*5

6*5

Because PowerShell is in argument parsing mode, it interprets “6*5” as an expression and displays it to the console host.

3. Now, type the following command in the PowerShell console:

PS C:\> Write-Host (6*5)

30

After processing Write-Host, PowerShell encounters the “(“ which causes it to switch to expression mode parsing. In this expression mode, it actually evaluates the mathematical expression of 6*5 and the output is 30.

4. The dollar sign is another one of the common characters that can cause PowerShell to enter expression mode parsing.

$int = 5 + 5

$int

You can see now that instead of treating the line of text as a command, PowerShell sees the dollar sign and knows that this is an expression to be evaluated, one which assigns the value of 5+5 into the variable “int”. On the second line, the evaluation is simply to retrieve the variable, which causes PowerShell to output that variable to the pipeline.

5. You can also force PowerShell into argument mode on a given expression. Enter the following commands:

$cmd = “get-command”

$cmd

We are simply using expression parsing mode and assigning a string into a variable.

Note that the “get-command” cmdlet is not actually executing, this is merely a text assignment.

6. Using the ampersand, also known as the “call operator”, we can now force the previous expression to be evaluated in argument mode instead.

&$cmd

This time the output is the same as if you directly executed the “get-command” cmdlet.

7. Script blocks are another example where PowerShell changes between parsing modes.

Execute the following code:

{ get-command –verb get }

This simply defines a script block, but does not execute it.

8. You can execute the code by putting the call operator in front of it:

& {get-command -Verb get}

Exercise 2: Basic Variable Types in PowerShell

Objectives

In this exercise, you will:

 Understand the type fundamentals in Windows PowerShell

 Learn the use of specific types of variables

Scenario

In PowerShell, you can create and delete objects for various purposes. In those objects, you can store data for performing some operations. Before you can store data, you may define the kind of data to be stored in that object. The kind of information that can be stored in an object is defined by its "Type".

Let us look at a few examples to understand this in detail.

In the exercise that follows, the use of different “Types” will be demonstrated.

 Number types

Task 1: Log on to VM Environment

Log on to the Windows 7 client as Contoso\Administrator.

In Windows PowerShell, some common "Types" are defined for Integer, Strings, and Boolean.

Task 2: Number Type

To understand numeric type, consider the following example:

To launch PowerShell, click on the taskbar.

1. Create a variable named, a and assign the value 9.

PS C:\> $a = 9

2. Here, you create a new variable and store a value of 9 in it. Now, let us see the type of variable.

3. To find the type of variable, use the GetType() method.

PS C:\>$a.gettype()

IsPublic IsSerial Name BaseType --- --- ---- ---

True True Int32 System.ValueType

4. Note the name, Int32. To hold the value, 9, you created an instance of "Type" of Int32 object. An Int32 object represents a 32-bit integer.

5. Change the value of the variable from an integer to a floating point.

PS C:\> $a = 4.5

PS C:\> $a.gettype()

IsPublic IsSerial Name BaseType --- --- ---- ---

True True Double System.ValueType

Note the Name property. It changed from Int32 to Double. An instance of "Type" of Double object represents a floating-point number.

6. Assign a larger value to the same variable.

PS C:\> $a =8000000000

PS C:\> $a.gettype()

IsPublic IsSerial Name BaseType --- --- ---- --- True True Int64 System.ValueType

Note that the Name property now shows Int64.

Let us take a few more examples. This time rather than assigning a math value let us take a list of all services on the computer.

7. In the PowerShell console, type the following command:

PS C:\> $a = Get-Service

PS C:\> $a.gettype()

IsPublic IsSerial Name BaseType --- --- ---- ---

True True Object[] System.ValueType

8. Similarly, store True or False in a Boolean object Type.

PS C:\> $a = 1

In the example above, you created two variables initially, assigned values to it and then did a compare operation. To store the result of the compare operation, you created a third variable and you can see that the "Type" is Boolean.

Task 3: String Type

PowerShell allows storing strings in variables. A string is a collection of characters. In the previous section, we looked at the different number types. In this section, you will get familiar with string types and their use.