PowerShell for Microsoft SharePoint 2010 : Variables in Windows PowerShell (part 4) - Automatic Variables , Preference Variables , Environment Variables

1/22/2014 11:25:03 PM

Automatic Variables

The Windows PowerShell automatic variables are fixed variables that store state information. Table 2 describes these variables.

Table 2. Windows PowerShell Automatic Variables
$$The last token in the last line received by the session
$?True or false, depending on the last performed operation
$^The first token from the last line received by the session
$_The current object in the pipeline object
$argsAn array of values passed to a function or a script
$ConsoleFileNameThe path of the console file that was most recently used in the session
$ErrorAn array of error objects representing the most recent error that occurred
$EventA PSEventArgs object that represents the event that is being processed
$EventSubscriberA PSEventSubscriber object that represents the event subscriber of the event that is being processed
$ExecutionContextAn EngineIntrinsics object that represents the execution context of the Windows PowerShell host
$falseBoolean false
$foreachThe enumerator of a foreach loop
$HomeUser’s home directory
$HostInformation regarding the current host
$inputAn enumerator that contains the input passed to a function
$LastExitCodeThe last exit code of the last program that was run
$MatchesResult of the last successful regular expression match
$MyInvocationInformation regarding the context under which the script, function, or script block was run
$NestedPromptLevelThe current prompt level
$NULLAn empty value
$PIDThe process identifier of the process that is hosting the current Windows PowerShell session
$ProfileThe full path to the Windows PowerShell profile for the current user
$PSBoundParametersA dictionary of the active parameters and their current values
$PsCmdletThe cmdlet that is being run
$PsCultureThe name of the culture currently in use
$PsDebugContextInformation about the debugging environment while debugging
$PsHomeThe full path of the installation directory of Windows PowerShell
$PsScriptRootThe directory from which the script module is being executed
$PsUICultureThe name of the user interface culture that is currently in use in the operating system
$PsVersionTableA read-only hashtable containing details about the version of Windows PowerShell
$PwdThe current directory
$ShellIDIdentifier of the current shell
$SourceArgsObjects that represent the event arguments of the current event
$SourceEventArgsThe first object that represents the first event argument that derives from EventArgs of the event that is being processed
$ThisReference to the current object in script methods and properties
$trueBoolean true

You can list the variables in Windows PowerShell by using the Get-ChildItem or Get-Variable cmdlet.

PS > Get-ChildItem Variable:
PS> Get-Variable

These commands return both automatic variables and user-created variables.

To display the value of a specific automatic variable, simply type the automatic variable’s name.


It is also possible to use the value of a variable as input to cmdlets in Windows PowerShell. In the next example, we use the value of the $PsHome variable with the Get-ChildItem cmdlet to retrieve all items from the Windows PowerShell home directory.

PS > Get-ChildItem $PSHOME

When working with pipelines and loops in Windows PowerShell, you use the automatic variable $_ to handle the current object in the pipeline. 

You can get more information about automatic variables by using the Get-Help cmdlet.

PS > Get-Help about_Automatic_variables

Preference Variables

Windows PowerShell includes a set of preference variables that let you customize its behavior. The preference variables affect the environment and how commands behave. Table 3 describes the preference variables available in Windows PowerShell.

Table 3. Windows PowerShell Preference Variables
$ConfirmPreferenceSets the level of impact that operations have before requesting confirmation: none, low, medium, or high
$DebugPreferenceControls how Windows PowerShell handles debugging messages written by a script or a cmdlet
$ErrorActionPreferenceSets the default error-handling action
$ErrorViewControls how Windows PowerShell should output errors to the shell
$FormatEnumerationLimitDetermines how many enumerated items are included in a display
$LogCommandHealthEventLogs errors and exceptions in command
 initialization and processing
$LogCommandLifeCycleEventLogs the start and stop of a command in a pipeline
$LogEngineHealthEventLogs session error and failures
$LogEngineLifeCycleEventLogs the opening and closing of a session
$LogProviderLifeCycleEventLogs adding and removing of providers in Windows PowerShell
$LogProviderHealthEventLogs provider errors
$MaximumAliasCountDetermines how many aliases are permitted in the current session
$MaximumErrorCountDetermines how many errors are saved in the error history
$MaximumFunctionCountDetermines how many functions are permitted in the current session
$MaximumHistoryCountDetermines how many commands are saved in the command history for the current session
$MaximumVariableCountDetermines how many variables are permitted in the current session
$OFSOutput field separator—specifies the character that an element in an array is separated with when the array is converted to a stringj
$OutputEncodingDetermines the default character encoding used by Windows PowerShell
$ProgressPreferenceDetermines how Windows PowerShell responds to progress updates generated by a script
$PSEmailServerSpecifies the default e-mail server used by Windows PowerShell cmdlets
$PSSessionApplicationNameSpecifies the default application name for a remote command that uses WS-Management technology
$PSSessionConfigurationNameSpecifies the default session configuration that is used for PSSessions created in the current session
$PSSessionOptionEstablishes the default values for advanced user options in a remote session
$VerbosePreferenceControls how Windows PowerShell handles verbose messages written by a script or a cmdlet
$WarningPreferenceControls how Windows PowerShell handles warning messages written by a script or a cmdlet
$WhatIfPreferenceDetermines if WhatIf is automatically enabled for every command that supports it

You can modify the preference variables by changing their value. For example, the following example changes the warning preference to silently continue.

PS > $WarningPreference = "SilentlyContinue"
PS > $WarningPreference

When you modify a preference variable in Windows PowerShell, the change affects only the current session. In order to make a persistent change to a preference variable, add the modification to a profile script, as this example shows:

'$WarningPreference = "SilentlyContinue"' | Out-File $PSHOME\profile.ps1 -Append

Environment Variables

Environment variables store information regarding the operating system environment. You can display the environment variables available using the Get-ChildItem cmdlet.

PS > Get-ChildItem env:

Here’s how to retrieve a specific environment variable:

PS > Get-ChildItem env:COMPUTERNAME

Name Value

---- -----

To display the value of the environment variable, add its name.


As with other variables, you can modify an environment variable by changing its value, as in this example:

PS > $env:COMPUTERNAME = "NewName"

As with preference variables, all changes made to the environment variables using Windows PowerShell affect only the current session. To permanently change environment variables, add the modifications to a profile script.


You can also permanently change environment variables through the SetEnvironmentVariable() static method available from the System.Environment class.

Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone
Visit movie_stars's profile on Pinterest.