You can do this in one line as follows:
$(select-string totaldisksize .\XML\*.xml).line -replace '.*totaldisksize="(\d+,\d+)".*','$1'
The Select-String
will give you a collection of objects that contains information about the match. The line
property is the one you're interested in, so you can pull that directly.
Using the -replace
operator, every time the .line
property is a match of totaldisksize
, you can run the regex on it. The $1
replacement will grab the group in the regex, the group being the part in parentheses (\d+,\d+)
which will match one or more digits, followed by a comma, followed by one or more digits.
This will print to screen because by default powershell will print an object to the screen. Because you're only accessing the .line
property, that's the only bit that's printed and also only after the replacement has been run.
If you wanted to explicitly use a Write-Host
to see the results, or do anything else with them, you could store to a variable as follows:
$sizes = $(select-string totaldisksize .\XML\*.xml).line -replace '.*totaldisksize="(\d+,\d+)".*','$1'
$sizes | % { Write-Host $_ }
The above stores the results to an array, $sizes
, and you iterate over it by piping it to the Foreach-Object
or %
. You can then access the array elements with $_
inside the block.