that outputs a object with the results
While that is technically true, it is more specifically an [object[]]
-typed array of lines ([string]
instances) that assigning the stream of output lines - produced by the external rclone
program - to a PowerShell variable implicitly created. (Arrays created by PowerShell are [object[]]
-typed, even if all the elements are of the same type, such as [string]
in this case).
PowerShell fundamentally only "speaks text" when communicating with external programs.
Therefore, to extract substrings from these lines you must perform text parsing, as implied by AdminOfThings' comment on the question.
A simplified approach is to use the unary form of the -split
operator:
# Simulate lines input whose first whitespace-separated token is to
# be extracted.
$getlist = 'foo bar baz', 'more stuff here'
$getlist.ForEach({ (-split $_)[0] })
The above yields:
foo
more
zett42's helpful answer shows a simpler alternative that relies on the -replace
operator's (among others) ability to operate directly on each element of an array-valued LHS.
However, the -split
approach is useful if you want to extract multiple column values.
If you don't need / want to capture all of the external program's (rclone
's) output in memory first, you can use streaming processing in the pipeline, via the ForEach-Object
cmdlet:
'foo bar baz', 'more stuff here' | ForEach-Object { (-split $_)[0] }
Note: While slightly slower than collecting all lines in memory up front, the advantage of a pipeline-based approach is reduced memory load: only the extracted substrings are kept in memory (if assigned to a variable).