Monthly Archive

Categories

Variables I

In my scripts I usually create variables as

$a = 3

or similar.

There are a number of cmdlets for working with variables

Clear-Variable
Get-Variable
New-Variable
Remove-Variable
Set-Variable

Lets start with get-variable

PS> Get-Variable

Name                           Value
----                           -----
$                              *variable
?                              True
^                              Get-Command
_
args                           {}
ConfirmPreference              High
ConsoleFileName
DebugPreference                SilentlyContinue
Error                          {}
ErrorActionPreference          Continue
ErrorView                      NormalView
ExecutionContext               System.Management.Automation.EngineIntrinsics
false                          False
FormatEnumerationLimit         4
HOME                           C:\Users\Richard
Host                           System.Management.Automation.Internal.Host.InternalHost
input                          System.Collections.ArrayList+ArrayListEnumeratorSimple
MaximumAliasCount              4096
MaximumDriveCount              4096
MaximumErrorCount              256
MaximumFunctionCount           4096
MaximumHistoryCount            64
MaximumVariableCount           4096
MyInvocation                   System.Management.Automation.InvocationInfo
NestedPromptLevel              0
null
OutputEncoding                 System.Text.ASCIIEncoding
PID                            5064
PROFILE                        C:\Users\Richard\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
ProgressPreference             Continue
PSBoundParameters              {}
PSCulture                      en-GB
PSEmailServer
PSHOME                         C:\Windows\System32\WindowsPowerShell\v1.0
PSSessionApplicationName       wsman
PSSessionConfigurationName    
http://schemas.microsoft.com/powershell/Microsoft.PowerShell
PSSessionOption                System.Management.Automation.Remoting.PSSessionOption
PSUICulture                    en-US
PSVersionTable                 {CLRVersion, BuildVersion, PSVersion, WSManStackVersion...}
PWD                            C:\scripts
ReportErrorShowExceptionClass  0
ReportErrorShowInnerException  0
ReportErrorShowSource          1
ReportErrorShowStackTrace      0
ShellId                        Microsoft.PowerShell
StackTrace
true                           True
VerbosePreference              SilentlyContinue
WarningPreference              Continue
WhatIfPreference               False

 

We can see a similar listing with

PS> Get-ChildItem -Path variable:

To view an individual variable we have

PS> Get-Variable -Name PWD

Name                           Value
----                           -----
PWD                            C:\scripts

PS> $variable:PWD

Path
----
C:\scripts

 

Notice the difference in the way the information is returned.  If we only want the value of the variable we need

PS> Get-Variable -Name PWD -ValueOnly

Path
----
C:\scripts

PS> $variable:PWD.Path
C:\scripts

 

If we dig into this a bit we see that

PS> Get-Variable -Name PWD | gm

   TypeName: System.Management.Automation.PSVariable

 

PS> Get-Variable -Name PWD -ValueOnly | gm

   TypeName: System.Management.Automation.PathInfo

 

PS> $variable:PWD | gm

   TypeName: System.Management.Automation.PathInfo

 

There is an extra wrapper when we are dealing with Get-Variable that we need to be aware of.

One other benefit to using Get-variable is that we can use the –scope parameter to examine the variables in specific scopes.

Technorati Tags: PowerShell,variables

Leave a Reply