Powershell Tutorial #1 – Using Variables
Welcome to the first powershell tutorial on Think Tree IT. This will be a 5 part tutorial. By the end of it all, you will be able to comfortable use many commands and create your own scripts. For this first blog post, we will explain how Powershell variables work.
With PowerShell you can create named objects known as variables. Variable names can contain a underscore character, letters, and also numbers. When a variable is used in PowerShell, it is always specified using the $ character before the variable name.
Assigning a Value
Its best to think of a variable like a container that stores information. It can contain a number, a letter, and many other data types. We assign a value to a variable using equal symbol(=).
$Variable = 1 $Variable = 20.133 $Variable = $true $Variable = "Hello there"
You can add, subtract, divide, and many more actions with variables.
Powershell Variable Test
- Try creating 2 variables, name them anything you want, then divide them and display the results.
- Create 2 variables. Let the first variable contain the world “Hello” and the second variable “World”. Combine them together so the output is “Hello World”.
- Create a variable named $Computer with your computer name or IP Address. Then run “Test-Connection $Computer”
Question 1: Pretty straight-forward, I just divided 100 by 20 and printed the $Results of 5.
Question 2: Same as above, except I added ” ” to add a space between the 2 words.
Question 3: I introduced a CMDlet in this one. It’s a powershell alternative to running ping 192.168.x.x
Feel free to leave a comment below with any suggestions or questions.
Interested In learning Powershell?
Check out “PowerShell in Depth“. PowerShell in Depth, Second Edition is the go-to reference for administrators working with Windows PowerShell. Every major technique, technology, and tactic is carefully explained and demonstrated, providing a hands-on guide to almost everything an admin would do in the shell. Written by three experienced authors and PowerShell MVPs, this is the PowerShell book you’ll keep next to your monitor—not on your bookshelf!
- Automating tasks
- Packaging and deploying scripts
- Introduction to Desired State Configuration
- PowerShell security
- Covers PowerShell version 3 and later
This post “Powershell Tutorial #1 – Using Variables” is located under the Programming Category