From a CMD shell, you can do either of the following, but make sure your batch file only echoes what you want to see in your output file. This will allow you to use tee
.
Using a batch file:
PowerShell.exe -Command ".\fetchDB.bat | tee 'filenameoutput.txt'"
Using commands that can be passed to PowerShell:
PowerShell.exe -Command "$sqlinstance='servername'; sqlcmd.exe -S $sqlinstance -d Test -i 'fileName.sql' | tee 'filenameoutput.txt'"
# You can use this option if your CMD shell has variable sqlinstance defined
powershell.exe -Command "sqlcmd.exe -S %sqlinstance% -d Test -i 'fileName.sql' | tee 'filenameoutput.txt'"
From a PowerShell console, you can just call the fully qualified batch file and pipe to tee
.
.\fetchdb.bat | tee "filenameoutput.txt"