programming4us
programming4us
WEBSITE

PowerShell for Microsoft SharePoint 2010 : Variables in Windows PowerShell (part 1) - Working with Variables

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us