I have a Powershell command that I want to be run in a batch script. It works well in the Powershell console, but I cannot call it properly in batch script. The Powershell command goes like this:

invoke-sqlcmd -inputfile "D:\T3 Reports\sql.sql" -ServerInstance ISD-DB02 | export-csv "D:\T3RawDataFiles\T3HistoricalRaw\Complaint\samp.csv" -Force -En UTF8

Why doesn't this command work in a batch script?

Also, is it possible to use a batch variable in place of the file path for input and output file instead of putting the whole path in the Powershell command (still run inside in batch script)?



I have found that works :) to run the invoke-sqlcmd within batch script:

I have use this line:

powershell -Command "& {Add-PSSnapin SqlServerCmdletSnapin100; Add-PSSnapin SqlServerProviderSnapin100; invoke-sqlcmd -inputfile '%sqlPath1%' -ServerInstance %Server% | export-csv '%out_path1%\%out1_fn%' -Force -En UTF8;}"

works like charm yay! ^_^

  • yup, that's the typical way to do it :-) – megamorf Mar 19 '15 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.