PowerShell Variable Scope

Just thought I’d blog about PowerShell variable scope. It seems that not many people are aware of this (I wasn’t aware until recently). There are scopes for private, local (this is the default behaviour), script and global (the PowerShell console).

$private:myVar = “Some value”
$local:myVar = “Some value”
$script:myVar = “Some value”
$global:myVar = “Some value”

This article contains a nice section with slightly more detail. http://powershell.com/cs/blogs/ebookv2/archive/2012/02/02/chapter-3-variables.aspx#scope-of-variables

You May Also Like

About the Author: rnowik

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.