0
0
PowerShellscripting~5 mins

Why variables store data in PowerShell

Choose your learning style9 modes available
Introduction
Variables hold information so you can use or change it later in your script. They keep data safe while your script runs.
When you want to remember a user's name to greet them later.
When you need to store a number to use in calculations.
When you want to save a file path to open or save files.
When you want to keep track of a status or choice during a script.
When you want to reuse a value multiple times without typing it again.
Syntax
PowerShell
$variableName = value
Variable names start with a $ sign.
You can store numbers, text, or other data types in variables.
Examples
Stores the text 'Alice' in the variable named $name.
PowerShell
$name = "Alice"
Stores the number 30 in the variable named $age.
PowerShell
$age = 30
Stores a greeting message using the value of $name.
PowerShell
$greeting = "Hello, $name!"
Sample Program
This script saves a name and age in variables, then prints them.
PowerShell
$user = "Sam"
$age = 25
Write-Output "User name is $user"
Write-Output "User age is $age"
OutputSuccess
Important Notes
Variables can change value anytime in the script.
Use meaningful variable names to make your script easy to read.
PowerShell variables are case-insensitive, so $User and $user are the same.
Summary
Variables store data so scripts can remember and use it.
Use $ sign to create variables in PowerShell.
Variables make scripts flexible and easier to write.