Learn to Use the PowerShell Variable: Drive

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Variable: drive.

Hey, Scripting Guy! Question Hey, Scripting Guy! What is a Variable: drive? It sounds somewhat “iffy.”

—AS

Hey, Scripting Guy! Answer Hello AS,

Microsoft Scripting Guy, Ed Wilson, is here. The Charlotte Windows PowerShell User Group meeting last night was great. The Scripting Wife and I had a great time, as did the other attendees. Windows PowerShell MVP, Jim Christopher, and user group supporter, Brian Wilhite, continue to amaze me at how they manage the group. They are doing a fine job.

This morning I am listening to a CD that the Scripting Wife bought me of Segovia. Along with a nice cup of Gunpowder green tea with jasmine, it creates the perfect Windows PowerShell scripting environment (at least for me).

Access the Variable: drive to find variables

To access the Windows PowerShell Variable: drive, I can use the Get-ChildItem cmdlet and specify the drive name. I can do this from any location. This technique is shown here.

PS C:\> dir variable:

 

Name                           Value

—-                           —–

$                              cls

?                              True

^                              cls

args                           {}

ConfirmPreference              High

<…Output Truncated…>

In general, however, when I am working with the Variable: drive, I set my working location to that drive as shown here.

PS C:\> cd variable:

PS Variable:\>

To see the variables that are defined in my session, I can use the Get-ChildItem cmdlet (dir, ls, and gci are aliases). This technique is shown here.

Image of command output

Modify variables by using the Variable: drive

To create a new variable, I can use the New-Item cmdlet. I must specify the name of the variable and the value to associate with it. This technique is shown here.

PS Variable:\> New-Item -Name sample -Value “sample value”

 

Name                           Value

—-                           —–

sample                         sample value

To directly access the value, I call the variable with the $ (dollar sign) in front of the variable name. This is shown here.

PS Variable:\> $sample

sample value

However, if I use Get-Item to retrieve the variable, I do not use the dollar sign. This is shown here.

PS Variable:\> Get-Item sample

 

Name                           Value

—-                           —–

sample                         sample value

To modify the value that is associated with the variable, I can use the Set-Item cmdlet as shown here.

PS Variable:\> Set-Item sample -Value “new sample value”

PS Variable:\> Get-Item sample

 

Name                           Value

—-                           —–

sample                         new sample value

If I want to delete the variable, I use the Remove-Item cmdlet. This technique is shown here.

PS Variable:\> Get-Item sample | Remove-Item

I can also use the Remove-Item cmdlet directly (and not use the pipeline). This is shown here.

PS Variable:\> New-Item -Name sample -Value “sample value”

 

Name                           Value

—-                           —–

sample                         sample value

 

PS Variable:\> Remove-Item sample

AS, that is all there is to using the Windows PowerShell Variable: drive. This also concludes Windows PowerShell Provider Week. Join me tomorrow for the Weekend Scripter when I will talk about finding dynamic parameters for your favorite Windows PowerShell cmdlets.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon