'Powershell write variable to stdout as running
In my powershell script I have a variable which captures its commands output
$files= dosomething
so running dosomething alone will produce output to the stdout. My question is how do I get that same functionality out of my variable as its running? mostly as a way to see progress.
maybe best to run the command and then somehow capture that to a variable?
UPDATE 1:
here's the command I'm trying to monitor:
$remotefiles = rclone md5sum remote: --dry-run --max-age "$lastrun" --filter-from "P:\scripts\filter-file.txt"
UPDATE 2:
running rclone md5sum remote: --dry-run --max-age "$lastrun" --filter-from "P:\scripts\filter-file.txt"
directly produces output ...
Solution 1:[1]
Tee-Object saves command output in a file or variable and also sends it down the pipeline.
Get-Process notepad | Tee-Object -Variable proc | Select-Object processname,handles
ProcessName Handles
----------- -------
notepad 43
notepad 37
notepad 38
notepad 38
This example gets a list of the processes running on the computer, saves them to the $proc
variable, and pipes them to Select-Object
.
Solution 2:[2]
Out-Host
will send output to the console without sending it down the pipe
Function Test-Output {
param()
$list = @(
'Dogs',
'Cats',
'Monkeys'
)
$list | Out-Host
$list | Select-Object -First 1
}
$output = Test-Output
# When running the line above Dogs, Cats, and Monkeys will display in the console
# Dogs
# Cats
# Monkeys
# However the variable $output will only contain 'Dogs'
$output
# Dogs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SADIK KUZU |
Solution 2 | Daniel |