Introduction

Variables are important to programming since they act as containers of information stored during the program execution. Although piping makes PowerShell highly versatile, they still cannot replace variables, because objects passed through the pipeline must be consumed immediately, and not all scripts work that way, given our varied requirements.

We are all aware of the various data types: int, double, string, char, array, etc. Two other important types of variables in PowerShell are hashtables and objects. A hashtable is a dictionary table formed with keyvalue pairs. An object, as we have seen, could be as complex and as simple as it can get in PowerShell, holding values of different kinds.

In PowerShell, objects can be stored into variables. For instance,

$Processes = Get-Process

would store all the processes into the variable, $Processes.

One more point to remember with variables is the scope. By default, variables have a local scope, meaning, they are valid within the function that they are specified in. Global variables are valid across the program. In general, it is a best practice to use local variables, though.

Global variables are declared and used with the $Global: prefix, such as $Global:MyVariable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.166.7