PowerShell for IT Professionals [#11] – Variables
In this lesson we’re going to learn how what Variables are, why it’s good to use them and how to actually create them. We will then assign various values to variables to and use them solely, and in parameters.
We will also have a look at foreach loop so that we can e.g. ping multiple computers using single variable with multiple objects in it.
Use Get-Command to find all commands that allow you to manage variables. Then find the command that let’s to display all currently available variables.
In notepad, store a few hostnames/ip addresses each in separate line
- Use Get-Content commandlet to read the content of the file just created
- Store the content of the file into a variable
- Display all the objects of variable – it should be exactly the same as content of the file
- Use foreach loop to ping all the servers from your variable
Common variable types
- [string] – Text string
- [char] – a single character
- [bool] – Boolean (can be either $True or $False)
- [int] or [int32] – 32-bit Integer
- [long] or [int64] – 64 – bit Integer
- [DateTime] – Date and time
Commands used in the lesson