How to prevent informational messages from getting written when query results are output to a file When I am running the below query as a batch file (.BAT) using the Windows Task Scheduler, I am getting some informative messages in the output file DatabaseNames.txt in addition to the database names. How can I prevent those messages? These messages are not visible when running from the Command prompt. 

"C:\Program Files\PostgreSQL\10\bin\psql.exe" --host localhost --username postgres -Atc "SELECT datname FROM pg_database WHERE datallowconn IS TRUE AND datname NOT IN('template0','template1')" &gt; D:\Backup\DatabaseNames.txt


Below were the informative messages


Welcome to PostgreSQL! 
Type :version to see PostgreSQL version 
Type :extensions to see the available extensions
Time: 0.994 ms
Apparently "someone" create a psqlrc.conf file that contains those messages you are seeing. 

You can prevent psql from loading psqlrc.conf by passing the parameter --no-psqlrc (or -X). To hide the any other messages use the --quiet (or -q) parameter. 

So your batch file should contain: 

"C:\Program Files\PostgreSQL\10\bin\psql.exe" --no-psqlrc --quiet --host localhost ...