Windows PowerShell technology enables the ability to execute administration commands and to fully program the algorithms in the language of PowerShell. To do this, PowerShell has attributes inherent in all modern programming languages such as variables, conditions, loops, arrays, error handling, and all this can be used to write complex administration scripts.
PowerShell is an object-oriented scripting programming language that is part of Windows PowerShell technology. The syntax of all PowerShell programming constructs is similar to the syntax of such languages.
Variables in Windows PowerShell
In Windows PowerShell, there’s both built-in variables and variables that the user can create. You can see all the variables in PowerShell using the Get-Variable cmdlet and using this cmdlet you can get the value of a variable but usually the variable is accessed via the dollar sign ($).
Variables in PowerShell can be declared with type and without. When declaring a variable it can be initialized immediately. Another feature of variables in PowerShell is that they can change their type depending on the value of this variable but this is only if we forcefully declare a variable type when declaring a variable.
In order to create a variable you must specify a dollar sign ($) before its name. If necessary you can force the data type of the variable in square brackets before its name.
Since PowerShell is an object-oriented programming language. It’s a complex object with properties and methods that can serve as a variable value. For example, the built-in variable $ PSVersionTable which contains service information about the version of PowerShell.
There are many types of these variables in Windows PowerShell, since they are based on the types that are in the .NET Framework. But at the same time there are also the most common types of data, i.e. they basic, such as:
- [int] is an integer, 32 bits;
- [single] – single precision floating point number;
- [double] – double-precision floating-point number;
- [string] is a string of characters, i.e. text;
- [char] – one character;
- [Boolean] – values of “True” or “False”;
- [datetime] is the date or time.
To work with variables, there are special PowerShell cmdlets:
- New-Variable – creating a variable;
- Set-Variable – assignment of a variable value;
- Get-Variable – get the variable;
- Clear-Variable – clearing a variable;
- Remove-Variable – delete by a change.
Conditional constructs in PowerShell
In PowerShell as with other programming languages there are also conditional constructions. They are needed in order to perform certain actions based on the result of the audit. The conditional constructions in PowerShell include the following constructions:
- IF … ELSE
- IF … ELSEIF … ELSE
Since conditional constructions assume a comparison of something it is necessary to touch on such a topic as comparison operators. Even more so since in PowerShell they are written differently from other languages.
PowerShell comparison operators
- -eq – equal (in other languages, usually =);
- -ne – not equal (in other languages equivalent to <> or! =);
- -gt – more (in other languages, usually>);
- -lt – less (in other languages, usually <);
- -ge is greater than or equal to (in other languages equivalent to> =);
- -le is less than or equal to (in other languages equivalent to <=).
Cycles in PowerShell
In PowerShell you can also use loops. A loop is a software construct that involves multiple execution of the same actions.
In Windows PowerShell, there are several types of cycles:
- DO WHILE
- DO UNTIL
Arrays in PowerShell
There are also arrays in PowerShell, an array is a data structure for storing a collection of items. Elements of an array can be of the same or different types such as the array can be either strongly typed or not.
As in many programming languages in PowerShell, an array can be accessed by an index (starting from zero). When initializing an array we list the values with a comma.
Error handling in Windows PowerShell
For tracking and interception of errors in Windows PowerShell there is a mechanism referred to as Try … Catch. It is a construction in which you put all the code in which an exception (an error) can occur then put into the Try block. In the Catch block you write the code that must be executed when an error occurs. If there were no errors the Catch block simply will not be executed.