官术网_书友最值得收藏!

Using variables

In PowerShell, the data is stored, retrieved, and modified using the methods listed next. In this section, we are going to cover only variables:

  • A variable is used to store bits of information
  • Arrays are used to store information in an index
  • A Hash table uses key-value pair to store data

A variable is virtual storage to store information that will be utilized later in a script or the information that is the output of running a script. Variables can contain different data types such as text strings, objects, and integers. There are few special variables that you can use, which are defined in PowerShell.

The examples of some of the special variables are as follows:

If you are looking for a full list of automatic variables available for use in Windows PowerShell, type the following:

PS C:\> get-help about_automatic_variables

In PowerShell, all the variable names must start with the $ character. Data is assigned to a variable using the = operator:

PS C:\> $Number = 25

There are the Set-Variable and Get-Variable cmdlets that can also be used to set and retrieve the values of the variable.

Here, the value 401B is passed to the variable ExamCode:

PS C:\> Set-Variable -Name ExamCode -Value 401B
PS C:\> Get-Variable ExamCode

A little bit about operators that are used for assignment and comparison of values assigned to variables.

Assignment operators

The following operators are used to assign values to variables and carry out numeric operations before the assignment:

The example assigns the value of to the variable called $Book:

$Bookk = "Microsoft Exchange PowerShell Essentials"

In the previous example, the $Book variable is created as we have assigned the value to the variable using the assignment operator. If you look at the following example, the first statement creates a $x variable and assigns a value of 12. The second statement has changed its value to 34:

$x = 12
$ = 34

While scripting, there is a need to combine operations such as addition, subtraction, and assignment to variables. For example, the following statements will produce the exact same output:

$i += 3
$i = ($i +3)

Similarly, if you want to multiply first and then assign, use any of the following statements:

$n *= 4
$n = ($n * 4)

If you want to use a specific data type, you have to override how PowerShell stores values in variables. This can be achieved by strongly typed variables or casting them. For example, the first one is a variable that will only store integers and the second one will store strings:

[int]$Number = 12
[string]$Book = "Microsoft Exchange PowerShell Essentials"

Now, let's take a look at an example of what happens if we do not specify the correct datatype to a variable. Here, we have stored the value of 56 in the $x variable as a string. The next statement adds the value of 8 to the variable. The output will be displayed as a string and numeric operation will be performed. As a best practice, you should always cast your variables with the correct data type:

[string]$s =  56
$s += 8
$s
568

You will not be able to recast a variable without changing its value. In our previous example, we have casted $Book as a string value and stored the value of Microsoft Exchange PowerShell Essentials in it. If you try to use the following statement, you will get an error message, which essentially means that you have to change the value before you cast the variable to a different data type:

[int]$Book 

Cannot convert value string to type System.Int32. Error—"Input string was not in a correct format."

At line:1 char:1
+ [int]$b 
+ ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

Now, let's use the Windows PowerShell help system to learn more about other assignment operators:

PS C:\> get-help about_assignment_operators

Comparison operators

These operators are used to compare values and match specific patterns. Here is the list of comparison operators available, and these operators are case insensitive. If you want to make these operators case sensitive or insensitive, precede c or i in front of the following operator:

  • eq
  • -ne
  • -gt
  • -ge
  • -lt
  • -le
  • -Like
  • -NotLike
  • -Match
  • -NotMatch
  • -Contains
  • -NotContains
  • -In
  • -NotIn
  • -Replace

Take a look at the following examples:

-eq—Equal to:

PS C:\> "apple" -eq "apple"
True
PS C:\> "apple" -eq "orange"
False

-ne—Not Equal to:

PS C:\> "apple" -ne "apple"
False
PS C:\> "apple" -ne "orange"
True

-gt—Greater than:

PS C:\> 5 -gt 8
False
PS C:\> 23,34,56 -gt 50
56

Like—Match using the wildcard character (*).

Take a look at the following examples:

PS C:\> "Windows PowerShell" -like "*shell"
True
PS C:\> "Windows PowerShell", "Server" -like "*shell"
 Windows PowerShell

Review the help for other comparison operators:

PS C:\> get-help about_comparison_operators
主站蜘蛛池模板: 普格县| 长葛市| 思茅市| 长乐市| 四子王旗| 西青区| 凤山县| 寿阳县| 会理县| 武邑县| 玉溪市| 台州市| 阳东县| 日喀则市| 桐庐县| 五莲县| 嘉禾县| 兴隆县| 商河县| 日喀则市| 山西省| 大城县| 宣化县| 绥中县| 阜南县| 通榆县| 高要市| 松滋市| 班玛县| 福清市| 鹤岗市| 札达县| 海林市| 禄劝| 扎鲁特旗| 永康市| 道孚县| 信丰县| 崇明县| 方城县| 沽源县|