Running Windows Powershell Scripts simply opens it in the editor
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.
Solution 1:
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"
Solution 2:
Run regedit
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell
Change the value of the (Default)
attribute to 0
.
The possible values are following:
-
0
- execute, -
Edit
- Open in PowerShell ISE, -
Open
- open in Notepad.
For security reasons Microsoft set the default action to Open
.
Solution 3:
I realized that this question is already answered, but if you really want to run .\filename.ps1, there's a way to do it.
Part 1: Get PowerShell Location
- Open Start > All Programs > Accessories > Windows Powershell.
- Right click Windows Powershell shortcut.
- Left click "Open file location".
- Highlight the location bar on the top.
- Right click the location bar.
- Left click "Copy".
Part 2: Set .ps1 File Association
- Go to the location of your .ps1 file.
- Right click the .ps1 file.
- Left click "Properties".
- Find the part where it said "Opens with:". Left click the "Change..." button on its right side.
- Left click the "Browse..." button on the bottom right.
- Highlight the location bar on the top.
- Right click the location bar.
- Left click "Paste".
- Left click the button with an arrow pointing right on the right side of the location bar.
- Left click "powershell.exe".
- Left click "Open"
- Go back to the "Open with" window.
- Left click "OK" on the bottom right.
- Go back to the properties window.
- Left click "OK" on the bottom right.
Part 3: Run .\filename.ps1 From Command Prompt
- Run this from command prompt.
.\filename.ps1
-Y.P.