IT tutorials
 
Technology
 

PowerShell for Microsoft SharePoint 2010 : Special Operators

12/10/2013 2:19:16 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Windows PowerShell includes special operators that you can use to perform tasks that cannot be performed by the other operators. Special operators in Windows PowerShell allow you to perform tasks such as dot-sourcing, creating arrays, and more. Table 1 lists the special operators that are available.
Table 1. Windows PowerShell Special Operators
OperatorNameDescription
..Range operatorIndicates a range of values; the first value in the range goes before the operator and the last value goes after it
&Call operatorRuns a command, script, or script block
.Dot-sourcing operatorRuns a script and includes the items, functions, and variables in the script to the current scope
::Static member operatorCalls the static properties operator and methods of a .NET Framework class
-fFormat operatorFormats strings by using the format method of string objects
$()Subexpression operatorReturns the result of an expression placed in a parenthetical list
@()Array subexpression operatorReturns one or more statements as an array
,Comma operatorAs a binary operator, creates an array; as a unary operator, creates an array with one member

The range operator (..) is used to retrieve a specified range from an array.

PS > $array = 1,2,3,4,5
PS > $array[0..2]
1
2
3

This example retrieves the elements with index 0 to 2.

The range operator also accepts negative ranges.

PS > $array[-1..-2]
5
4

The call operator (&) in Windows PowerShell is used to run commands, scripts, or script blocks. One thing to keep in mind is that the call operator does not interpret parameters. This example demonstrates how to run a command stored in a variable and represented by a string:

PS > $myCommand = "Get-SPWeb"
PS > & $myCommand -Identity http://SPServer01

Url
---
http://spserver01

Here, we use "Get-SPWeb" as the input string to the variable and specify the arguments when we call the variable.

You can also use the call operator to run script blocks. A script block can contain any amount of code and is defined by braces.

PS > & { 1 + 1 }
2

The dot-sourcing operator (.) is used to include variables and functions from a script to the current scope. This means that you can store functions and variables in a script and use the dot-source notation to quickly access the functions and variables contained in the script. Here is an example:

PS > $variableInScript
PS > . .\myScript.ps1
PS > $variableInScript
This variable is placed in the myScript.ps1 script

Here, we first try to call the variable $variableInScript. Since we have not created a variable called $variableInScript in our current session, the command does not return a value. Next, we dot-source our script, which contains the variable $variableInScript. When we call the variable a second time, a value is returned.

The static member operator (::) is used to call static methods and properties of a .NET Framework class. To find out static methods and properties of a class, use the Get-Member cmdlet with the Static switch parameter.

PS > [System.Math] | Get-Member -Static

Here are some examples of using the static member operator with the static methods and properties of the System.Math class:

  • Use the static method Pow() to return the specified number raised to the specified power:

    PS > [System.Math]::Pow(5,5)
    3125
  • Use Sqrt() to calculate the square root of 9:

    PS > [System.Math]::Sqrt(9)
    3
  • Call the static property PI, which represents the ratio of a circle to its diameter:

    PS > [System.Math]::PI
    3,14159265358979

The format operator (-f) is a binary operator that uses the same formatting rules as the Format() method in the .NET Framework. It takes a string on the left side and an array of values on the right side. Here is an example:

PS > "{0}" -f "PowerShell"
PowerShell

In this example, the value enclosed in braces represents the index of the element on the right side. Since the format operator allows an array on the right side, you can use more values.

PS > "{0}" -f "PowerShell","Windows"
PowerShell

With two elements on the right side but one on the left, the operator returns only the first element. If you add a second value on the left side, you can retrieve both values:

PS > "{0} {1}" -f "PowerShell","Windows"
PowerShell Windows

You can also switch the places of the numeric values to change the output.

PS > "{1} {0}" -f "PowerShell","Windows"
Windows PowerShell

The subexpression operator ($()) returns the result of one or more statements. If the result contains multiple values, an array is returned. The next example demonstrates how to use the subexpression operator.

PS > "There are: $((Get-Command -Noun SP*).Count) SharePoint cmdlets available
in Windows PowerShell"
There are: 531 SharePoint cmdlets available in Windows PowerShell



The array subexpression operator (@()) returns the result of one or more statements as an array. You can use the operator to create a simple array, as shown in this example:

PS > @("Windows","PowerShell")
Windows
PowerShell

You can access elements in the array by their index.

PS > @("Windows","PowerShell")[0]
Windows
PS > @("Windows","PowerShell")[1]
PowerShell

The comma operator (,) is a binary operator used to create simple arrays in Windows PowerShell.

PS > 1,2,3
1
2
3

You can also use the comma operator to place a single line in an array.

PS > $array = ,"PowerShell"
PS > $array[0]
PowerShell

When you place a comma operator in front of a value, Windows PowerShell treats the value as an element in an array. When you place the statement in a variable and call the first element in the array, the string value is returned.

 
Others
 
- PowerShell for Microsoft SharePoint 2010 : Type Operators
- PowerShell for Microsoft SharePoint 2010 : Redirection Operators
- PowerShell for Microsoft SharePoint 2010 : Logical Operators
- PowerShell for Microsoft SharePoint 2010 : Comparison Operators
- PowerShell for Microsoft SharePoint 2010 : Assignment Operators
- PowerShell for Microsoft SharePoint 2010 : Arithmetic Operators
- Microsoft Systems Management Server 2003 : Updating Collections (part 3) - Collection Evaluator Update Process Flow, Status Messages
- Microsoft Systems Management Server 2003 : Updating Collections (part 2) - Deleting a Collection
- Microsoft Systems Management Server 2003 : Updating Collections (part 1) - Forcing an Update
- Implementing Exchange Server 2010 Security : Configuring Standard Permissions for Exchange Server (part 3) - Assigning Advanced Exchange Server Permissions
 
 
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
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us