Understand scopes in PowerShell – Part 2

This is continuation of part 1 of understand scopes in PowerShell. Consider following PowerShell code:

Set-Alias XDate Get-Date
XDate

function fun1{
 XDate
}

function fun2{
 Set-Alias XDate Get-Location
 fun1
}

fun2
XDate

function fun3{
 fun1
 Set-Alias XDate Get-Location 
}

fun3
XDate

As I don’t need to explain anymore, the output is:

Tuesday, December 27, 2016 10:18:53 AM

Drive : C
Provider : Microsoft.PowerShell.Core\FileSystem
ProviderPath : C:\Windows\system32
Path : C:\Windows\system32

Tuesday, December 27, 2016 10:18:53 AM
Tuesday, December 27, 2016 10:18:53 AM
Tuesday, December 27, 2016 10:18:53 AM

The Using scope modifier

Using is a special scope modifier that identifies a local variable in a remote command. By default, variables in remote commands are assumed to be defined in the remote session.

For example, the following commands create a $Cred variable in the local session and then use the $Cred variable in a remote command:

$Cred = Get-Credential 

Invoke-Command $s {Remove-Item .\Test*.ps1 -Credential $Using:Cred}

Other than this, $using is used most commonly in PowerShell workflows to make variables visible in the inline script.

Managing Scope Explicitly (and using Numbered scopes)

Many cmdlets have a Scope parameter that lets you get or set (create and change) items in a particular scope. You can use the following command to find all the cmdlets in your session that have a Scope parameter:

get-help * -parameter scope

You have seen example of this in blog post 1 where we used to identify variables in the local and global scope.

Also mentioned in the previous blog post that you can refer to scopes by name or by a number that describes the relative position of one scope to another. Scope 0 represents the current, or local, scope. Scope 1 indicates the immediate parent scope. Scope 2 indicates the parent of the parent scope, and so on. Let’s see this in action.Considers below PowerShell code:

$x = "testvar"

function fun1{
 $x
}

fun1

function fun1{
 Set-Variable -Name x -Value "testvar2" -Scope 0
 $x
}

fun1

function fun2{
 Set-Variable -Name x -Value "testvar3" -Scope 1
 $x
}

fun2

And the output is:

testvar
testvar2
testvar3

Using Private Scope Variables

For this, all you have to do is set the scope to Private. For example, this command creates a variable named $x with the scope set to private:

$Private:x = “testvar”
$x

Because this is a private variable it will be available only in the scope in which it was created.

Using Dot Source Notation with Scope

I’m sure you are familiar with using the dot source notation to import scripts in the current session. What happens is after this, any functions, aliases, and variables that the script creates are available in the current scope.

To understand its effect, let’s declare a variable called $x in current session like this:

PS C:\Windows\system32> $x = “testvar”

PS C:\Windows\system32> $x
testvar

Now, save following code in a script called scope.ps1:

$x = “testvar2”

Write-Output “the value of x is $x”
Using dot source notation, import the script in current session:

PS C:\Windows\system32> . C:\Users\goyal_m\Desktop\scope.ps1
the value of x is testvar2

Now, if you want to check the value of $x again, it is overwritten as testvar2 as expected:

PS C:\Windows\system32> $x
testvar2

One thought on “Understand scopes in PowerShell – Part 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s