Variables & Data Types
What is a Variable?
A variable is like a named container that holds a piece of data. You can store a value in it and refer to it later by name.
In PowerShell, all variables start with a dollar sign $. This makes them easy to spot.
"...") are automatically replaced with their values. Variables inside single quotes ('...') are treated as literal text โ no substitution happens.Data Types
PowerShell is loosely typed โ it figures out the type automatically. But it's good to understand what types exist:
String Operations
Strings have many useful built-in methods. Since everything in PowerShell is an object, you call methods with a dot:
Numbers and Math
Arrays
An array holds multiple values in a single variable. Think of it as a numbered list.
Hashtables
A hashtable stores key-value pairs โ like a dictionary or a lookup table. Keys are unique and you use them to retrieve values.
Type Conversion
Sometimes you need to convert between types:
String Formatting with -f
The -f format operator lets you insert values into a string template cleanly:
๐งช Try It Yourself
- Create a variable
$myNamewith your name and display it - Create an array of 5 of your favorite things
- Create a hashtable describing yourself (name, age, city)
- Use string interpolation to build a sentence using your variables
- Try
"hello".ToUpper()and" spaces ".Trim()
Key Takeaways
- Variables always start with
$ - Use
=to assign, just refer to the name to read - Double quotes allow variable interpolation; single quotes are literal
- Arrays use
@()or comma-separated values; index starts at0 - Hashtables use
@{}withkey = valuepairs - Everything in PowerShell is an object with properties and methods