IT tutorials
 
Technology
 

PowerShell for Microsoft SharePoint 2010 : Comparison Operators

12/10/2013 2:16:49 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
The comparison operators are used to compare values, as well as to find values that match specific patterns. Table 1 lists the comparison operators available in Windows PowerShell.
Table 1. Windows PowerShell Comparison Operators
OperatorDescription
-eqEqual to
-neNot equal to
-gtGreater than
-ltLess than
-leLess than or equal to
-geGreater than or equal to
-likeMatch using the wildcard character (*)
-notlikeDoes not match using the wildcard character (*)
-matchEvaluates a regular expression against the operand on the left; returns True if the match is successful
-notmatchEvaluates a regular expression against the operand on the left; returns True if the match is not successful
-replaceReplaces all or part of a value with the specified value using regular expressions

The -eq operator returns True or an array of matching values if it can match the value on the right with one or more values on the left. The operator returns True if an exact match is made.

PS > "http://SPServer01" -eq "http://SPServer01"
True
PS > "http://SPServer01" -eq "http://SPServer01/Site"
False

In the first example, we use the –eq operator to compare two identical strings. In the second example, we add a few lines to the value on the right. Since the strings are not identical, False is returned.

We can also use the –eq operator to match an argument on the right side with an array of values on the left.

PS > "http://SPServer01", "http://SPServer02" -eq "http://SPServer02"
http://SPServer02

The -ne operator returns True if the left value and the right value are not identical. If the left side of the operation contains multiple values, the operator returns ones that do not match the value on the right.

PS > "http://SPServer01" -ne "http://SPServer01"
False
PS > "http://SPServer01","http://SPServer02" -ne "http://SPServer01"
http://SPServer02

The -gt operator returns True if the left value is greater than the right value. The -ge operator returns True if the left value is greater than or equal to the right value.

PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount
9
PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount -ge 9
True
PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount -gt 9
False



In this example, we use the Get-SPSiteAdministration cmdlet to retrieve the number of users of a site. In this case, there are nine users. We then use the –ge operator to check if the number of users is greater than or equal to nine, which returns True. Next, we check if the number of users is greater than nine, which returns False.

To see if the left value is less than the right value, use the -lt operator. The –le operator is for less-than or equal-to comparisons.

PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount -lt 9
False
PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount -le 9
True



The –like and –notlike operators are similar to the –eq and –ne operators, but instead of matching exact values, they allow wildcards to be used.

PS > "http://SPServer01" -like "http://SPServer01"
True
PS > "http://SPServer01/Site" -like "http://SPServer01*"
True

In the first example, we use the –like operator to compare two identical strings. Since the strings match, True is returned. In the second example, we use a wildcard character when we compare the values. The wildcard character matches any given character. If the left side of the operation contains multiple values, the operator returns matching values, rather than True or False, as shown in the next example.

PS > "http://SPServer01/internal/site1",
>> "http://SPServer01/external/site1" -like "*external*"
>>
http://SPServer01/external/site1

For case-sensitive evaluation, use the –clike operator, as demonstrated here:

PS > "http://SPServer01" -clike "http://SPServer01"
True
PS > "http://SPServer01" -clike "http://SPSERVER01"
False

The following examples demonstrate how to use the –notlike operator.

PS > "http://SPServer01" -notlike "http://SPServer01"
False
PS > "http://SPServer01/Site" -notlike "http://SPServer01"
True
PS > "http://SPServer01/Site" -notlike "http://SPServer01*"
False

If the left side of the operation contains multiple values, the operation returns values that do not match, as shown here:

PS > "http://SPServer01/internal/site1",
>> "http://SPServer01/external/site1" -notlike "*external*"
>>
http://SPServer01/internal/site1

The –match and –notmatch operators try to match one or more of the set of string values on the left side of the operation using regular expressions. Regular expressions are a very powerful means of pattern matching (not specific to PowerShell or any other technology), which allow you to create very complex and effective comparisons. Here are some examples:

  • Match http://SPServer01/Site with http://SPServer01:

    PS > "http://SPServer01/Site" -match "http://SPServer01"
    True
  • Match http://SPServer01/Site with a regular expression that checks if the string starts with http://:

    PS > "http://SPServer01/Site" -match "^http://"
    True
  • Check if http://SPServer01/Site starts with http://, followed by any number of alphanumeric characters, followed by /, followed by any number of alphanumeric characters and ends with an e:

    PS > "http://SPServer01/Site" -match "^http://\w*/\w*e$"
    True

As you can see, it is possible to build up complex match patterns using regular expressions. Similar to the –clike operator, -cmatch performs case-sensitive matching.

You can also use multiple values on the left side and match against a value on the right side, as shown in this example:

PS > "http://SPServer01/Site",
>> "http://SPServer01",
>> "http://SPServer02" -match "http://SPServer01"
>>
http://SPServer01/Site
http://SPServer01

This example returns the left values that match the right value.

The -replace operator in Windows PowerShell is used to find and replace substrings in a string value. It assumes its input is a regular expression. You can use the -replace operator to search for and replace a specific pattern.

PS > "http://SPServer01/Site" -replace "^http","https"
https://SPServer01/Site

Here, we replace http with https. We use the ^ character to match the beginning of the original string.

Here is an example that uses a more complex replacement pattern:

PS > "http://SPServer01/Site" -Replace "/{2}\w*/","//SPServer02/"
http://SPServer02/Site

This example compares the string "http://SPServer01/Site" to the specified pattern and replaces the part of the string that starts with two / characters, contains any number of alphanumeric characters, and ends with another /.

Note

Unlike the -replace operator, the Replace() method available through System.String does not accept regular expressions.

 
Others
 
- 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
- Implementing Exchange Server 2010 Security : Configuring Standard Permissions for Exchange Server (part 2) - Assigning Standard Exchange Management Permissions
- Implementing Exchange Server 2010 Security : Configuring Standard Permissions for Exchange Server (part 1) - Understanding the Exchange Management Groups
- LINQ to SharePoint and SPMetal : Combining LINQ to SharePoint and LINQ to Objects
- Windows Server 2008 and Windows Vista : Preferences (part 6) - Network Security
 
 
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