Ironman Software Forums
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
In this blog post, we’ll look at the basics of assigning variables.
The basic assignment uses the $
character in front of the variable name.
$Variable = 123
Set-Variable
You can use the Set-Variable
cmdlet to set variables.
Set-Variable -Name 'Variable' -Value 123
You can change the scope of a variable by using the scope prefix.
Global scope is available anywhere in the current PowerShell environment. Script scope is available to the current script file or module.
$Global:Variable = 123
$Script:Variable = 123
Set-Variable -Name Variable -Value 123 -Scope Global
Set-Variable -Name Variable -Value 123 -Scope Script
You can use Set-Variable
with complex names. You can also use curly braces and the $
prefix to reference variables with characters such as spaces.
Set-Variable -Name 'This is a variable with spaces' -Value 123
${This is a variable with spaces} = 123
You can create read only variables with Set-Variable
.
Set-Variable -Name 'Variable' -Value 123 -Option ReadOnly
Set-Variable
allows you to add descriptions to your variables. You can view variable descriptions with Get-Variable
.
Set-Variable -Name 'Variable' -Value 123 -Description 'This variable has a description'
Find this useful? Please consider sharing this article. Have a question about PowerShell? Contact us and we'll write a post about it.
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
Receive once-a-month updates about Ironman Software. You'll learn about our product updates and blogs related to PowerShell.