Numeric assignments using the multiply and assign operator may be performed using *=. The value held by the following variable i will be 4:
$i = 2 $i *= 2
The multiply and assign operator may be used to duplicate a string held in a variable:
$string = 'one' $string *= 2
The value on the right-hand side of the *= operator must be numeric or must be able to convert to a number. For example, a string containing the number 2 is acceptable:
$string = 'one' $string *= '2'
Using a string that is unable to convert to a number results in an error as follows:
PS> $variable = 'one'
$variable *= 'one'
Cannot convert value "one" to type "System.Int32". Error: "Input string was not in a correct format."
At line:2 char:1
+ $variable *= 'one'
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
The multiply and assign operator may be used to duplicate an array held in a variable. In the following example, the variable will hold the values 1, 2 , 1, and 2 after this operation:
$variable = 1, 2 $variable *= 2
The assign and divide operator is used to perform numeric operations. The variable will hold a value of 1 after the following operation:
$variable = 2 $variable /= 2
The modulus and assign operator assigns the result of the modulus operation to a variable:
$variable = 10 $variable %= 3
After the preceding operation, the variable will hold a value of 1, the remainder when dividing 10 by 3.