0

I have a Powershell script which grabs the codecs used by various video files. The Write-Host command is working correctly and I can see the desired output in the terminal. But the Out-File part is not working and only creates an empty file.

Can anyone tell me why the Out-File is not working?

$codec = ffprobe -i $vidfile -hide_banner -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1

Write-Host "Processing" $vidfile  "Codec:" $codec -Append | Out-File -Filepath "Z:\Movies\codecs.txt"
Paolo
  • 21,270
  • 6
  • 38
  • 69
zBernie
  • 97
  • 2
  • 10
  • 2
    [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it _by itself_; e.g., `$value` instead of `Write-Host $value` (or use `Write-Output $value`). See the linked duplicate for more information. – mklement0 Jan 15 '23 at 14:09
  • If I use Write-Output the output is not displayed to the terminal. I'm trying to display the output to the terminal and also to a file. -Thanks – zBernie Jan 15 '23 at 15:16
  • To both output to the display and capture in a file, use [`Tee-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/tee-object). If you need further help, please ask a _new_ question. – mklement0 Jan 15 '23 at 16:17
  • Ditto for Tee-Object, but, know that you can assign value to a variable and output to the screen (no Write-* needed) using [PS variable squeezing](https://duckduckgo.com/?q=%27powershell+variable+squeezing%27&t=h_&ia=web) as well. For example: ```"Processing $vidfile Codec: $($codec = ffprobe -i $vidfile -hide_banner -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1)";$codec | Out-File -Filepath 'Z:\Movies\codecs.txt' -Append```. Note: I am contacting with the ```;```, in order to fit in the comment section, otherwise, it'd be multi-line. – postanote Jan 15 '23 at 20:35

0 Answers0