In this lecture, we are going to learn about PowerShell variables.
So, we are going to do most of our work today in the PowerShell ISE, although you can use either one that you want.
So, what I am going to do is open up the ISE.
One thing to keep in mind is that I am running this as an Administrator, and this is important because some of you might have PowerShell execution policies that may block scripts, so one way to get around that is to run the PowerShell as an Administrator and you can change the execution policy.
Again, we are not going to get into that in this lecture, but just so you understand how my environment is set up I wanted to make sure I am in that clear.
So, first, let’s talk about what is actually a variable? Well, is something that is not fixed or doesn’t have a fixed pattern and is liable to change, right?
So, in our old math classes we learned x = 1 and if we do x + x = 2, and x * x = 1 because what we are essentially doing is basic math, we all understand what this actually means.
Well in PowerShell also use variables, and we won’t only be using them to store basic math like basic integer numbers. We can also store strings and different things like that and it is extremely useful. So just let’s get right at it and get started.
So, how do you actually declare a variable? Well, in PowerShell is as simple as using the dollar sign $ or pressing the SHIFT+4 key combination and then declaring the name, so we can type:
And, if we run this in ISE hitting Play we will see initially just declared a variable (1). Now, if we echo the variable typing on the bottom pane in Powershell:
We are going to get a blank output (2) because we haven't assigned any type of value to my variable.
We could type down in the PowerShell pane:
$MyVariable = “Test”
We now get Test as an output.
Now, another way that we can output this data is simply just writing in the name of the variable $MyVariable and pressing Enter.
Let’s make a new variable and we will call it $processes = Get-Process, and if we run this we are not going to get any result but if we type echo $processes and press Enter we are going to get the output of the command Get-Processes which was stored in $processes.
Now, if I type Get-Processes and press Enter we will get the exact same output. The reason why it is useful to store information in a variable is that it can capture specific states of our computer at a specific point in time, we can manipulate these variables later on, etc.
If we want to manipulate the data that was stored in $processes we can just type the variable name and we can pipe it to a Select-Object command and as you know Select-Object will allow us to choose for example the Id or the ProcessName by typing $processes | Select-Object Id,ProcessName and press Enter.
So, you are able to manipulate this information, just like when running the Get-Process command.
One of the useful things you can do when processing variables is that sometimes you want to get the user input.
Let’s create a variable called $UserInput = Read-Host -Prompt “Please enter your name” and press Enter it will execute and ask us to enter our name.
Now, Paul is now stored inside $UserInput. So, if we type echo $UserInput and press Enter. We are going to get the value of $UserInput which is equal to the Read-Host command.
Sign up to access the rest of this lesson
You must either log in or sign up to access this lesson.