In this lab we’ll utilize the techniques from the previous lessons to:
- Learn about PowerShell Variables
- Compare variables containing collections of objects
- Create our own custom object using
.json and learn to parse through it using ForEach-Object
Let’s start with PowerShell Variables. PowerShell variables will always start with a $. They can contain any object or ‘collection of objects’ returned when you run a cmdlet.
Let’s start with a simple String Object. (We’ll be using PowerShell ISE for this.)
Type:
$mystring = "Praise be to Powershell!"
$mystring

In this example, we set the value of $mystring to equal a string value and print it back by typing the variable name.
We can also store returned objects in variables like this:
(Be sure the calculator is closed when you run this!)
$current_processes = get-process
$current_processes

We can now work with this ‘collection’ of objects stored in the $current_processes variable as we would the output of the get-process command itself.
For Example:
$current_processes | select Name,CPU | sort CPU

Now start the calculator (do this first). Then we’ll create a variable called $processes_new that contains the collection of objects returned from the get-process cmdlet with the calculator process running.
$processes_new = get-process