Running Windows Powershell Scripts simply opens it in the editor

powershell

I have a Windows powershell script that works fine in the interactive editor. The script is a simple one line sql cmd:

sqlcmd -S servername -d dbname -E -W -w 999 -s "," -Q "SELECT select col1,col2,'','','','','','','','','','','','','','','','','','','','','','','','','' From Table" -o "C:\sqlcmd.csv"

When I enter that at the powershell command prompt it works fine. I save it in a ps1 file and try to run it from the cmd prompt by typing .\filename.ps1, it opens it in Notepad, and does not execute it.

I then try to run it as a command like this:

powershell sqlcmd -S servername -d dbname -E -W -w 999 -s "," -Q "SELECT select col1,col2,'','','','','','','','','','','','','','','','','','','','','','','','','' From Table" -o "C:\sqlcmd.csv"

And that says "-s missing parameter…".

Any suggestions on getting this to run right? I read something somewhere about Windows execution policy and was wondering if it was something like that.

Operating System is Windows XP, SP2.

Best Answer

You can run the Powershell script from the command prompt like this:

powershell -command "& .\filename.ps1"

You may need to change your execution policy to run Powershell scripts.

powershell -command "Set-ExecutionPolicy Unrestricted"