Search code examples
powershellhttp-redirectcommand-linewindows-10output

What's wrong with the redirection of Powershell output?


I'm writing a simple command/script for monitoring memory usage in Powershell, it looks as follows:

PS C:\Temp_Folder> while ($true) {
>> Get-Date
>> TaskList /FI "PID eq 82660" | findstr /I "Prod"
>> Start-Sleep 1 }

This command/script shows the results on the prompt, but as I expect a lot of results, I would like to output to a file, so this is what I did (see this official URL):

PS C:\Temp_Folder> while ($true) {
>> Get-Date
>> TaskList /FI "PID eq 82660" | findstr /I "Prod"
>> Start-Sleep 1 } 2>&1 >>.\Geheugen.txt

However, this seems not to work:

PS C:\Temp_Folder> dir Geheugen.txt
dir : Cannot find path 'C:\Temp_Folder\Geheugen.txt' because it does not exist.
At line:1 char:1
+ dir Geheugen.txt
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Temp_Folder\Geheugen.txt:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

I already tried with and without error redirection (2>&1 and using append and overwrite (>> and >)), but no luck.
I know for a fact that the command/script is working: I see the results on the prompt.

What am I doing wrong?

Edit after some investigation:
Apparently, there is an Out-File command, which should be used as follows:

PS C:\Temp_Folder> while ($true) {
>> Get-Date
>> TaskList /FI "PID eq 82660" | findstr /I "Prod"
>> Start-Sleep 1 } 2>&1 | Out-File -FilePath .\Geheugen.txt

This is quite confusing:
In DOS (Windows commandline), redirection to a file can always be done, using the standard redirection operators > and >>. Apparently those operators are foreseen in Windows Powershell too, but sometimes they don't work. Does anybody have an exact description of that sometimes?

Extra test:
The Out-File seems not to be working, as you can see:

 while ($true) {
>> Get-Date
>> TaskList /FI "PID eq 4240" | findstr /I "Prod"
>> Start-Sleep 1 } | Out-file -FilePath .\Prod_Server_Monitoring.txt

I get following error message:

At line:4 char:17
+ Start-Sleep 1 } | Out-file -FilePath .\Prod_Server_Monitoring.tx ...
+                 ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

Solution

  • Weird. On my 7.4.0 your first example does not returns any error, using your exact same code.

    It also does not write the file but that's normal: you are attempting to write the success and error streams of the while ITSELF, not of the CONTENTS of the code it runs.

    You'd EITHER need to enclose the whole while(){} in a variable (es:$(while{}) >> 'file.txt') or a scriptblock (es: & { while(){} } >> 'file.txt'

    If you want to keep the terminal output, though, you might need to play with Tee-Object.

    Edit from question author:
    I've put the while-loop inside a variable and that solves the issue indeed:

    $(while ($true) { 
    ...
    }) >>.\Prod_Server_Monitoring.txt