TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Arithmetic Operators

PowerShell Arithmetic Operators with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

Arithmetic Operators

The arithmetic operators are used in a PowerShell to perform the calculation of the numeric values. By using the arithmetic operators, we can add (+), subtract (-), multiply (*), or divide (/) the values. These operators also include the modulus (%) operator to calculate the remainder of the division operation.

In PowerShell, the addition and multiplication operator may also be used with the strings, hash tables, and arrays. The multiplication (*) operator returns more than one copy of the input, and addition (+) operator concatenates the input.

Windows PowerShell supports the following arithmetic operators:

PowerShell Arithmetic Operators
  1. + (Addition)
  2. - (Subtraction)
  3. * (Multiplication)
  4. / (Division)
  5. % (Modulus)

Arithmetic Operators Precedence

PowerShell processes these type of operators in expression according to the following precedence rules:

PowerShell Arithmetic Operators

Addition Operator (+)

The addition operator is an arithmetic operator, which is used to add the two numbers, strings, arrays, and hash tables.

Examples:

Example1: This example adds two numeric values:

$a = 10
$b = 20 
$c = $a + $b
$c

The last command will display the value of the variable $c as 30.

Example2: This example adds two strings:

$x = "power"
$y = "Shell" 
$z = $x + $y
$z

The last command will display the value of the variable $z as a single string "PowerShell".

Example3: This example adds two arrays:

$x = 1,2,3
$y = "A","B","C"
$z = $x + $y
$z

The last command will display the following result of an array $z:

1
2
3
A
B
C

Subtraction Operator (-)

The subtraction operator is an arithmetic operator, which is used to subtract one numeric value from another numeric value and make a number to a negative number.

Examples:

Example1: This example subtract one numeric value from other value:

$a = 20
$b = 10 
$c = $a - $b
$c

The last command will display the value of the variable $c as 10.

Example2: This example makes a number to a negative number:

$x = 10
$y = -$x
$y

The last command will display the value of the variable $c as -10.

Multiplication Operator (*)

The Multiplication operator is an arithmetic operator, which is used to multiply the numeric values or copy the string and array values to the specified number of times.

Examples:

Example1: This example multiplies two numeric values:

$a = 10
$b = 20 
$c = $a * $b
$c

The last command will display the value of the variable $c as 200.

Example2: This example is used to create the copies of the string according to a number which is specified after the multiplication operator:

$x = "Shell"
$y =$x*2
$y

The last command will display the value of the variable $y as a single string "ShellShell".

Example3: This example is used to create the copies of array values according to a number which is specified after the multiplication operator:

$x = 1,2,3
$y = $x * 2
$y

The last command will display the following result of an array $y:

1
2
3
1
2
3

Division Operator (/)

The Division operator is an arithmetic operator which is used to divide the two numeric values.

The following example divides the two numeric values:

$a = 10
$b = 4 
$c = $a/$b
$c

The last command will display the value of the variable $c as 2.5.

Modulus Operator (%)

The modulus operator is an arithmetic operator which is used to calculate the remainder of the division operation

The following example calculates the modulus of the two numeric values:

$a = 10
$b = 4 
$c = $a % $b
$c

The last command will display the value of the variable $c as 2.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf