How to get 2 variables from a file PowerShell
Solution 1:
To refer to the first line in the file, you want $Contents[0]
($Contents[1]
would refer to the second line).
$a,$b = -split $Contents[0] -as [int[]]
Using -split
in unary mode will make PowerShell split on any sequence of consecutive whitespace characters, and throw away any empty parts (this way it works when the iput has leading or trailing whitespace, like " 3 5 "
).
The -as [int[]]
operation will force PowerShell to attempt to convert the resulting string values to [int]
values, so now you can meaningfully do integer arithmetic with them:
PS ~> $a + $b
8