2

Powershell version info below:

Name                           Value
----                           -----
PSVersion                      5.0.10586.494
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.494
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

I have the following powershell code:

Write-Host "Step 1..."

$find = "string-to-find"

Get-ChildItem -Path D:\path\path\ -Include *.dat1, *.dat2, *.dat3, *.dat4, -Recurse `
 | Select-String -SimpleMatch $find `
 | Select-Object -Unique Path

Write-Host "Step 2 ..."

Output:

Step 1...

Step 2...

Path
----
D:\path\path\test.dat1

Basically, the output from Get-ChildItem is occuring AFTER the subsequent Write-Host statement -- why????

Given that this code worked just fine in previous versions -- what is the correct output method one should use to have the output displayed in the order it is executed?

Thanks in advance.

Still trying to get this to work using the following:

Get-ChildItem -Path D:\path\path\ -Include *.dat1, *.dat2, *.dat3, *.dat4, -Recurse `
 | Select-String -SimpleMatch $find `
 | Select-Object -Unique Path `
 | ForEach-Object { $_ } | Write-Host

But the output looks like:

@{Path=D:\path\path\something.dat1}
@{Path=D:\path\path\something.dat1}

All I want is the full path name listed (like it was working before v5).

bdcoder
  • 3,280
  • 8
  • 35
  • 55
  • Because [`write-host` and `write-output` are different](http://stackoverflow.com/questions/8755497/which-should-i-use-write-host-write-output-or-consolewriteline), and `write-output` calls [are implicit](http://stackoverflow.com/a/19754384/478656). You're sending two different types of output, through two completely different routes (which might never actually end up on the screen together at all), then asking why it takes different amounts of time. – TessellatingHeckler Jul 26 '16 at 23:06
  • Thanks for the replies so far... But given this script WAS working just fine in previous versions of powershell -- what is the correct output method to have things output in order as they did previously?? – bdcoder Jul 26 '16 at 23:27
  • [Using `Write-Host` is almost always wrong.](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/) – Ryan Bemrose Jul 27 '16 at 01:07
  • Solved -- changed all "Write-Host" to "Write-Output" according to this post: http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/ – bdcoder Jul 27 '16 at 01:14

2 Answers2

1

Old question, but adding my answer since it's one of the top results on Google.

If you want to keep using Write-Host, say due to it's colouring abilities, it can be done by explicitly piping to Format-Table. The complete code would be:

Write-Host "Step 1..."

$find = "string-to-find"

Get-ChildItem -Path D:\path\path\ -Include *.dat1, *.dat2, *.dat3, *.dat4, -Recurse `
 | Select-String -SimpleMatch $find `
 | Select-Object -Unique Path `
 | Format-Table

Write-Host "Step 2 ..."

Get-ChildItem (and similar commands), or a pipeline which does not end in a variable\file, finally outputs to the console via an implicit call to Write-Output.

The console output appears out of order because Write-Output doesn't write to the console synchronously, unless you explicitly call Format-Table on Write-Output.

References:
https://serverfault.com/questions/693549/powershell-write-host-not-synchronous
https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/14925213-bug-console-output-appears-out-of-order

Dhruv Murarka
  • 376
  • 5
  • 12
0

Solved -- changed all "Write-Host" to "Write-Output" according to this post: jsnover.com/blog/2013/12/07/write-host-considered-harmful

bdcoder
  • 3,280
  • 8
  • 35
  • 55