0
0
PowerShellscripting~5 mins

Variable creation with $ in PowerShell

Choose your learning style9 modes available
Introduction

Variables store information you want to use later in your script. Using $ helps you create and use these variables easily.

When you want to save a user's name to use multiple times.
When you need to store a number to do math later.
When you want to keep a file path to open or save files.
When you want to remember a choice the user made.
When you want to hold temporary data during a script run.
Syntax
PowerShell
$variableName = value

Variable names start with $ followed by letters, numbers, or underscores, but cannot start with a number.

You can store text, numbers, or other data types in variables.

Examples
This creates a variable $name and stores the text "Alice".
PowerShell
$name = "Alice"
This creates a variable $age and stores the number 30.
PowerShell
$age = 30
This creates a variable $isReady and stores a true/false value.
PowerShell
$isReady = $true
Sample Program

This script creates two variables, $greeting and $name, then combines them into $message. Finally, it prints the message.

PowerShell
$greeting = "Hello"
$name = "Bob"
$message = "$greeting, $name!"
Write-Output $message
OutputSuccess
Important Notes

You can change a variable's value anytime by assigning a new value.

Use double quotes "" to include variables inside text strings.

Variable names are not case sensitive in PowerShell.

Summary

Use $ to create variables in PowerShell.

Variables store data like text or numbers for later use.

You can combine variables inside strings using double quotes.