filmov
tv
PowerShell 05: variables, operators, flow control statements

Показать описание
The basic building blocks for most programming languages are variables, operators, and flow control statements. This tutorial will cover them all and give us a good foundation for further exploration of other PowerShell features.
To define a variable in PowerShell, add dollar ($) sign in front of the variable name. The variable name is not case-sensitive, different from C, C++, C# or Python. For good practice, you should only include alphanumeric characters and the underscore character (_) in variable name. But in PowerShell, if you really want to include spaces or special characters in variable name, you can actually do it by enclosing the variable name in braces.
To assign an array to a variable, you can use @ sign and round brackets. To assign a hash table, or dictionary to a variable, use @ sign followed by curly brackets.
(demo starts)
Let's put what we have learned about variables into action.
First, define a variable $val in lower cases. Assign 10 as its value. Now Add 5 to $VAL. Notice we use upper cases. The result is 15, as expected. It also proves the variable name is case-insensitive.
Now let assign an array 1, 2, 3 to variable $array1. Display $array1. We can also use @() to assign array to $array2. We get the same result. By the way, we can use .count to get number of elements in array.
How to get more info on variables? Use get-help *variable*. Then copy the exact name: get-help about_variables. Now Y you can see all the details about variables in PowerShell.
(demo ends)
The next topic is operators. In PowerShell, the arithmetic operators, assignment operators are similar to those used in C/C++/C#. But comparison operators are quite different. For equal, use -eq, for not equal, use -ne. -lt means less than, -gt means greater than.
There are other kinds of operators. For more details, refer to the help system. We will take a look at them in the following demo.
One thing worth mentioning here is the difference between putting comparison objects inside single or double quotes. When a string is put inside double quotes, any variable inside the string will be expanded. On the other hand, if the string is put inside single quotes, any variable inside the string is treated as literals. We will review the difference in the demo.
(demo starts)
(demo ends)
The last topic I want to cover is flow control statements. I include condition and looping under flow control statements. Other documents separate them as different topics. I prefer remembering them as different aspects to control the program flow. Most of the time, they are used together to control loops based on conditions.
Condition statements are similar to other programming languages: if ... elseif ... else. You can get more details with about_If help file.
Looping statements have two major flavors: for, foreach are basically used for a specific number of array or object. While, do ... while, do ... until are used to iterate as long as condition is true or until it is met. You can find out all the details with several help files, like about_for, about_foreach, about_while, about_do.
(demo starts)
Code to demo if statement:
$a = 5
if ($a -lt 10)
{
Write-Host 'less than 10'
}
elseif ($a -lt 20)
{
Write-Host 'less than 20'
}
else
{
Write-Host 'greater than or equal to 20'
}
Code to demo foreach statement:
cls
$path = 'c:\tutorial'
foreach ($file in ls $path -Recurse)
{
$file | select fullname, creationtime, lastwritetime, lastaccesstime
}
(demo ends)
Here are some highlights of what we have learned in this tutorial. Define variable with $ sign. Comparison operators are different from other languages like C/C++/C#. Flow control statements include two categories: condition with if, looping with for, foreach, while, do while.
In the next tutorial, we will use what we have learned to create a useful PowerShell script like touch utility in Linux system.
Thanks for watching!
To define a variable in PowerShell, add dollar ($) sign in front of the variable name. The variable name is not case-sensitive, different from C, C++, C# or Python. For good practice, you should only include alphanumeric characters and the underscore character (_) in variable name. But in PowerShell, if you really want to include spaces or special characters in variable name, you can actually do it by enclosing the variable name in braces.
To assign an array to a variable, you can use @ sign and round brackets. To assign a hash table, or dictionary to a variable, use @ sign followed by curly brackets.
(demo starts)
Let's put what we have learned about variables into action.
First, define a variable $val in lower cases. Assign 10 as its value. Now Add 5 to $VAL. Notice we use upper cases. The result is 15, as expected. It also proves the variable name is case-insensitive.
Now let assign an array 1, 2, 3 to variable $array1. Display $array1. We can also use @() to assign array to $array2. We get the same result. By the way, we can use .count to get number of elements in array.
How to get more info on variables? Use get-help *variable*. Then copy the exact name: get-help about_variables. Now Y you can see all the details about variables in PowerShell.
(demo ends)
The next topic is operators. In PowerShell, the arithmetic operators, assignment operators are similar to those used in C/C++/C#. But comparison operators are quite different. For equal, use -eq, for not equal, use -ne. -lt means less than, -gt means greater than.
There are other kinds of operators. For more details, refer to the help system. We will take a look at them in the following demo.
One thing worth mentioning here is the difference between putting comparison objects inside single or double quotes. When a string is put inside double quotes, any variable inside the string will be expanded. On the other hand, if the string is put inside single quotes, any variable inside the string is treated as literals. We will review the difference in the demo.
(demo starts)
(demo ends)
The last topic I want to cover is flow control statements. I include condition and looping under flow control statements. Other documents separate them as different topics. I prefer remembering them as different aspects to control the program flow. Most of the time, they are used together to control loops based on conditions.
Condition statements are similar to other programming languages: if ... elseif ... else. You can get more details with about_If help file.
Looping statements have two major flavors: for, foreach are basically used for a specific number of array or object. While, do ... while, do ... until are used to iterate as long as condition is true or until it is met. You can find out all the details with several help files, like about_for, about_foreach, about_while, about_do.
(demo starts)
Code to demo if statement:
$a = 5
if ($a -lt 10)
{
Write-Host 'less than 10'
}
elseif ($a -lt 20)
{
Write-Host 'less than 20'
}
else
{
Write-Host 'greater than or equal to 20'
}
Code to demo foreach statement:
cls
$path = 'c:\tutorial'
foreach ($file in ls $path -Recurse)
{
$file | select fullname, creationtime, lastwritetime, lastaccesstime
}
(demo ends)
Here are some highlights of what we have learned in this tutorial. Define variable with $ sign. Comparison operators are different from other languages like C/C++/C#. Flow control statements include two categories: condition with if, looping with for, foreach, while, do while.
In the next tutorial, we will use what we have learned to create a useful PowerShell script like touch utility in Linux system.
Thanks for watching!