0

The output of this code:

$var1 = @("ServerA","ServerB","ServerC")
$var2 = @("ServerX","ServerY","ServerZ")

Write-Host "`nShow me var1`n"
$var1
Write-Host "`nShow me var2`n"
$var2

looks like this:

enter image description here

But if I use "Import-Csv" for the variables:

$var1 = Import-Csv .\Servers1.csv
$var2 = Import-Csv .\Servers2.csv

Write-Host "`nShow me var1`n"
$var1
Write-Host "`nShow me var2`n"
$var2

The output looks like this:

enter image description here

Why does it combine the outputs when I use Import-Csv?

How do I get the second output to look like the first one, where it outputs the values of var1 and var2 separately?

3
  • 1
    This is a fairly common question. Write-Host and Write-Output (default) use different streams. They are in no way forced to be synchronous. The short answer is to stop using Write-Host. In general Write-Host is the wrong answer and should only be used when you have a specific need.
    – EBGreen
    May 23, 2018 at 14:28
  • Yeah odd if you put the $var2 declaration line after the Write-Host line for var 1, does it show the correct output? Or does it combine var1 results there too? May 23, 2018 at 14:28
  • You should test it and report back May 23, 2018 at 14:36

1 Answer 1

0

Yeh, it's a quirk of console/host output. And yes writing to the host is generally not a good idea but there are times when it's all that's required:

If you do this instead you'll find it works fine:

$var1 = Import-Csv .\Servers1.csv
$var2 = Import-Csv .\Servers2.csv

Write-Host "`nShow me var1`n"
$var1 | Out-Host
Write-Host "`nShow me var2`n"
$var2 | Out-Host
5
  • Why say that Write-Host is generally not a good idea then provide that as the answer. Why not use the output stream as the answer since by your own statement that is the prefered method?
    – EBGreen
    May 23, 2018 at 15:01
  • Because, AS I SAID, there are rare times when it is genuinely what you want to do. Sheesh May 23, 2018 at 15:06
  • That's fine but what you have done is said "It is rare for you to need to do it this way. I'm going to assume that you do. Even though there is a high probability that this answer is actually the wrong way to do this."
    – EBGreen
    May 23, 2018 at 15:16
  • I just think it is internally inconsistent to say "This is almost always the wrong way to do it so do it this way" that is all. I feel that a better answer would be "This is most likely the best way to do it".
    – EBGreen
    May 23, 2018 at 15:21
  • I agree that writing to the console is not always the best, or most useful way to output data, but as you mentioned, there are some use cases where that's what you need to do, and I happen to be dealing with one, and this does what I needed it to do, so thanks for the help!
    – Brian
    May 23, 2018 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.