Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Variable: drive.
Hey, Scripting Guy! What is a Variable: drive? It sounds somewhat “iffy.”
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).
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:
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:
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.
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"
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
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
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"
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.
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 firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy