programming4us
programming4us
WEBSITE

PowerShell for Microsoft SharePoint 2010 : Variables in Windows PowerShell (part 1) - Working with 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:17:33 PM

Windows PowerShell, like most other scripting languages, stores values in variables. Variables are represented by single-word text strings that begin with the dollar sign ($). Windows PowerShell supports four types of variables: user-created, automatic, preference, and environment variables.

Working with Variables

The simplest way to create a variable is by using the assignment operator = to set a variable to a specific value. The following example creates the variable $string and assigns it the value My String.

PS > $string = "My String"

You can also use the New-Variable cmdlet to create variables. This cmdlet offers some additional functionality, such as adding a description, setting the variable to read-only or constant, and setting a specific scope for a variable. Here is an example:

PS > New-Variable -Name string -Value "My String" `
>> -Description "Created using New-Variable" -Option ReadOnly -Force

This creates the variable $string and sets the value to My String. We also specify a description, use the Option parameter to set the variable to ReadOnly, and use the -Force switch parameter since the variable already exists.

If we try to assign a new value to the variable using the assignment operator, an error occurs since the variable is set to read-only.

PS > $string = "New Value"
Cannot overwrite variable string because it is read-only or constant.
At line:1 char:8
+ $string <<<< = "New Value"
+ CategoryInfo : WriteError: (string:String) [],
SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable

Instead, we can use the Set-Variable cmdlet with the -Force switch parameter to set a new value. We also change the Option parameter from ReadOnly to None.

PS > Set-Variable -Name string -Value "New Value" -Option None -Force

The Get-Variable cmdlet is used to retrieve variables, as follows:

PS > Get-Variable -Name string

Name Value
---- -----
string New Value

This example returns an object of the type System.Management.Automation.PSVariable, which contains properties that describe the variable. If you want to display only the variable’s value, use the -ValueOnly switch parameter.

PS > Get-Variable -Name string -ValueOnly
New Value

This example returns an object of the type System.String. This corresponds to just typing a dollar sign ($) followed by the variable’s name:

PS > $string
New Value

You can display additional information about a variable by using the Get-Variable cmdlet and sending the object to the Format-List cmdlet.

PS > Get-Variable -Name string | Format-List
Name : string
Description : Created using New-Variable
Value : New Value
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}

To clear the value of a variable, use the Clear-Variable cmdlet.

PS > Clear-Variable -Name string

When using the Clear-Variable cmdlet, the variable’s value is set to null. You can also set a variable to null using the assignment operator.

PS > $string = $null

To delete a variable, use the Remove-Variable cmdlet.

PS > Remove-Variable -Name string

Note

You cannot delete variables set as constants or variables owned by the system.

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