Forum Discussion
Output in CLI different that what is saved in log file ?!
Hi, Touqueer.
Superficially, it looks like an encoding issue with the file, as that's what a Unicode stream output to an ANSI file tends to appear like.
Separately, Tee-Object is not technically positioned correctly.
The idea behind Tee-Object is that it splits one input channel into two destinations: a file and then the standard pipeline. Pipeline redirection is not required in this scenario.
With that in mind, simply:
- Construct the string;
- Pipe it to Tee-Object first;
- Let Tee-Object work as intended, where it sends the input to a file as well as the standard output;
- Leave Write-Warning last, which takes the standard pipeline input and writes it to the warning pipeline.
Example
"Boo!" | Tee-Object -FilePath "D:\Data\Temp\Forum\warning.log" | Write-Warning;
Output (screen)
Output (file)
Cheers,
Lain
I tried this and it works fine but the output saved in the file did not change and remains the same which bring in Encoding.
I came to know 2 things.
1) There is a $PSDefaultParameterValues = @{ '*:Encoding' = 'UTF8' }. The questio is if this sets Encoding of the whole script from the start (added to the start of the script), or does it change the Encoding of which ever fils is created durin gscript runtime ?
2) Add-Content has an -Encoding switch but I'm not sure how this would work with Tee-Object since Add-Content adds to the file, and Tee-Object adds to the file and displays in the CLI as well ?!
- LainRobertsonMar 19, 2024Silver Contributor
Hi, Touqueer.
You can remove the value from PSDefaultParameters as shown below - which you would need to do since Tee-Object offers no control over encoding.
Reference
Examples
Once you've removed the relevant encoding entry from $PSDefaultParameterValues, try your command again - the file output should now be fine.
Cheers,
Lain