Simply outputting something is PowerShell is a thing of beauty - and one its greatest strengths. For example, the common Hello, World! application is reduced to a single line:
"Hello, World!"
It creates a string object, assigns the aforementioned value, and being the last item on the command pipeline it calls the .toString() method and outputs the result to STDOUT (by default). A thing of beauty.
The other Write-* commands are specific to outputting the text to their associated streams, and have their place as such.
Simply outputting something is PowerShell is a thing of beauty - and one its greatest strengths. For example, the common Hello, World! application is reduced to a single line:
"Hello, World!"
It creates a string object, assigns the aforementioned value, and being the last item on the command pipeline it calls the .toString() method and outputs the result to STDOUT (by default). A thing of beauty.
The other Write-* commands are specific to outputting the text to their associated streams, and have their place as such.
I think in this case you will need Write-Output.
If you have a script like
Write-Output "test1";
Write-Host "test2";
"test3";
then, if you call the script with redirected output, something like yourscript.ps1 > out.txt, you will get test2 on the screen test1\ntest3\n in the "out.txt".
Note that "test3" and the Write-Output line will always append a new line to your text and there is no way in PowerShell to stop this (that is, echo -n is impossible in PowerShell with the native commands). If you want (the somewhat basic and easy in Bash) functionality of echo -n then see samthebest's answer.
If a batch file runs a PowerShell command, it will most likely capture the Write-Output command. I have had "long discussions" with system administrators about what should be written to the console and what should not. We have now agreed that the only information if the script executed successfully or died has to be Write-Host'ed, and everything that is the script's author might need to know about the execution (what items were updated, what fields were set, et cetera) goes to Write-Output. This way, when you submit a script to the system administrator, he can easily runthescript.ps1 >someredirectedoutput.txt and see on the screen, if everything is OK. Then send the "someredirectedoutput.txt" back to the developers.
PowerShell Write-Output: Your Friendly Output Companion
Write output to a file
In this script, why is Write-Output not behaving the way I expect?
Write-Host vs Write-Output in Functions?
Go right to the source.
Write-Verboseor Write-Progress for information about progress. Write-Output for stuff going to the pipeline.
If you're using v5, there's Write-Information too.
Can Write-Output be used to write to a file?
How do I prevent overwriting a file?
Videos
Hey guys,
Nicholas Xuan Nguyen just wrote a shiny new blog post you may enjoy.
"PowerShell Write-Output: Your Friendly Output Companion"
Summary: Learn all of the ins and outs of the PowerShell Write-Output cmdlet and take control of script and object output in this ATA Learning tutorial!
https://adamtheautomator.com/powershell-write-output/
Hi there. I have a PowerShell script that I run from our ConfigMgr server that gets me device names when someone provides me with a list of user names.
Right now the script works, but it only outputs all the info to the PowerShell window. I can't get the output to write correctly to a file. I either get just the user names or just the device names, but can't get both.
Here's my latest version of the script:
$a = Get-content "C:\Temp\users.txt"
foreach ($name in $a)
{
$name = "Corp\" + $name
"\n" + $name + "`t"; Get-CMUserDeviceAffinity -UserName "$name" | Select-Object {$_.ResourceName} -ExpandProperty ResourceName | Out-File C:\Temp\userdevice.txt -append`
}
This will write the user names to the PowerShell window and write the device names to my specified output file. That's about as close as I've got to the desired result, which is that I'd like to have each line of my result file contain:
user name [tab character] device name