Programming on PowerShell language
- January 13, 2016

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:

  1. [int] is an integer, 32 bits;
  2. [single] – single precision floating point number;
  3. [double] – double-precision floating-point number;
  4. [string] is a string of characters, i.e. text;
  5. [char] – one character;
  6. [Boolean] – values of “True” or “False”;
  7. [datetime] is the date or time.

To work with variables, there are special PowerShell cmdlets:

  1. New-Variable – creating a variable;
  2. Set-Variable – assignment of a variable value;
  3. Get-Variable – get the variable;
  4. Clear-Variable – clearing a variable;
  5. 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
  • IF … ELSE
  • IF … ELSEIF … ELSE
  • SWITCH

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:

  1. WHILE
  2. DO WHILE
  3. DO UNTIL
  4. FOR
  5. FOREACH

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.

recommended
Smart Order Router. What is this?

One of the key features of FinSafe’s trading platform is Smart Order Routing. A professional trading platform that a traditional …

9 useful abbreviations in the JavaScript syntax

Various shortcuts increase your productivity and make the JavaScript code shorter. And since short records are absolutely equivalent to long …

Several reasons to explore Swift

Swift is applied to create applications for Mac and iOS acting as a possible replacement for Objective-C. The latter, though …