IT tutorials
 
Technology
 

PowerShell for Microsoft SharePoint 2010 : Arithmetic Operators

12/10/2013 2:15:10 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

The basic arithmetic operators include those to add, multiply, subtract, divide, and calculate the remainder of a division. Table 1 lists these operators.

Table 1. Windows PowerShell Arithmetic Operators
OperatorDescription
+Adds two values
-Subtracts one value from another
*Multiplies two values
/Divides one value by another
%Returns the remainder from a division

Let’s take a closer look at the + operator. To add the values 1 and 5 together, you could type this:

PS > 1 + 5
6

To add a string with a numeric value, use this form:

PS > "String" + 5String5

It is also possible to add multiple string values to build up a single string. This example uses the + operator to build up a URL from three strings:

PS > "http://" + "SPServer01" + "/MySite"
http://SPServer01/MySite

You can also add string objects stored in variables together.

PS > $url = "http://SPServer01"
PS > $web = "MySite"
PS > $url + "/" + $web
http://SPServer01/MySite

However, it is not possible to add a string to a numeric value.

PS > 1 + ";#" + "Item"
Cannot convert value "String" to type "System.Int32".
Error: "Input string was not in a correct format."
At line:1 char:4
+ 1 + <<<< "String"
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

Windows PowerShell interprets the first argument as an instance of the type System.Int32. When we try to add a System.String value to a System.Int32 value, an error occurs. Windows PowerShell expects an argument of the type System.Int32, and it is not possible to convert a System.String value containing characters other than numeric ones. The following is the correct way to add the values:

PS > "1" + ";#" + "Item"
1;#Item

You can also cast the numeric value using the [string] type literal, which is a PowerShell alias for the System.String type.

PS > [string]1 + ";#" + "Item"
1;#Item

The value on the left side defines the type of the whole operation. You can add a number to a string, since a number can be converted to a string value, as shown in this example:

PS > "#Item" + 1
#Item1

Here are examples of using the other arithmetic operators:

  • Use the – operator to subtract numeric values:

           PS > 5 - 4
    1
  • The - operator also works with negative numbers:

           PS > -1 - 1
    -2
  • Use the * operator to multiply values:

           PS > 5 * 5
    25
  • You can also multiply string values with a numeric value:

           PS > "Hello" * 5
    HelloHelloHelloHelloHello
  • Divide numeric values with the / operator:

           PS > 9 / 3
    3
  • Use the modulus (%) operator to calculate remainders:

           PS > 10 % 3
    1
    PS > 6 % 2
    0
 
Others
 
- 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
- Windows Server 2008 and Windows Vista : Preferences (part 5) - Hardware Components
- Windows Server 2008 and Windows Vista : Preferences (part 4) - Servers
 
 
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