I tried to make a one liner that executes a command and than prints out a message, for example:
dir; echo 1
which outputs:
PS C:\Users\Administrator\Documents> dir; echo 1
Directory: C:\Users\Administrator\Documents
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/18/2021 3:27 PM a
d----- 8/18/2021 3:27 PM vdb.1_1.dir
-a---- 8/30/2021 2:48 PM 12 a.txt
-a---- 8/30/2021 2:54 PM 2044 dir.txt
-a---- 8/30/2021 10:31 AM 8 hey.txt
1
PS C:\Users\Administrator\Documents>
When running only dir, there are two blank lines at the end:
PS C:\Users\Administrator\Documents> dir
Directory: C:\Users\Administrator\Documents
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/18/2021 3:27 PM a
d----- 8/18/2021 3:27 PM vdb.1_1.dir
-a---- 8/30/2021 2:48 PM 12 a.txt
-a---- 8/30/2021 2:54 PM 2044 dir.txt
-a---- 8/30/2021 10:31 AM 8 hey.txt
PS C:\Users\Administrator\Documents>
But the combination of two commands seems to ruin the order (The '1' is printed before the two blank lines).
When I ran other versions the problem did not occur, even when the first command printed blank lines:
PS C:\Users\Administrator\Documents> echo 0; echo 1
0
1
PS C:\Users\Administrator\Documents>
PS C:\Users\Administrator\Documents> echo hello`n`n; echo 1
hello
1
PS C:\Users\Administrator\Documents>
What causes it? Does it happen with other commands too (besides ls, Get-ChildItem or other aliases)? How can I bypass that (the output is supposed to go to a script that assumes the message is at the end)?
** I'm running Windows server 2019, PowerShell version 5.1.17763.316
($(dir *.xml;echo 1) | Out-String -Stream) -ne ''. I would say this is not really PowerShell idiomatic. There's probably a better way to tackle this whole problem if you state the overall goal in the question.($(echo a) | Out-String -Stream) -ne '', a is not printed andTrueis, I supposed becauseais indeed not equal to ''. Why hasn't that occur whendirwas there too? Is there any workaround to always just get the desired output?@($(dir *.xml;echo 1) | Out-String -Stream) -ne ''.