0
0
PowerShellscripting~5 mins

Assignment operators in PowerShell

Choose your learning style9 modes available
Introduction
Assignment operators let you store or update values in variables easily.
When you want to save a number or text to use later.
When you want to add or subtract from a value already stored.
When you want to combine text stored in variables.
When you want to quickly update a variable without rewriting it fully.
Syntax
PowerShell
$variable = value
$variable += value
$variable -= value
$variable *= value
$variable /= value
$variable %= value
The '=' operator sets a variable to a value.
Operators like '+=' add to the current value of the variable.
Examples
Sets the variable $count to 10.
PowerShell
$count = 10
Adds 5 to the current value of $count.
PowerShell
$count += 5
Sets $name to 'John' then adds ' Doe' to make 'John Doe'.
PowerShell
$name = "John"
$name += " Doe"
Sample Program
This script shows how to set and update numbers and text using assignment operators.
PowerShell
$score = 20
$score += 10
$score -= 5
$message = "Hello"
$message += ", World!"
Write-Output "Score: $score"
Write-Output "Message: $message"
OutputSuccess
Important Notes
Assignment operators work with numbers and strings in PowerShell.
Using '+=' with strings joins them together.
Be careful with division and modulo operators to avoid errors with zero.
Summary
Assignment operators store or update values in variables.
Use '=' to set a value, and operators like '+=' to add or combine.
They make scripts shorter and easier to read.