programming4us
programming4us
WEBSITE

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

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
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
VariableContents
$$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.

PS > $PSHOME
C:\Windows\System32\WindowsPowerShell\v1.0

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
VariableDescription
$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
SilentlyContinue

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

---- -----
COMPUTERNAME SPSERVER01

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

PS > $env:COMPUTERNAME
SPSERVER01

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

PS > $env:COMPUTERNAME = "NewName"
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.

Note

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

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

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

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