23

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.

0

4 Answers 4

6

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"
4
  • 3
    This didn't work for me. Get-ExecutionPolicy already returned "Unrestricted" for me, and then running powershell -command &'C:\code\redwoodblog\node_modules\.bin\redwood' still just opened a js file.
    – Ryan
    Apr 25, 2020 at 23:26
  • Can you explain how to run multiple commands and wait for each command to finish before running the next command? May 13, 2020 at 6:20
  • @Wolfpack'08 simply put && in between them.
    – 71GA
    Nov 29, 2023 at 7:13
  • @71GA Between what? This code was working previously. I can't test it right now I'm on linux. Can you verify that the current answer isn't working? Dec 27, 2023 at 17:31
14

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.

2
  • 3
    This didn't work for me. I even closed PowerShell after editing the Registry and reopened it (as Admin). &'C:\code\redwoodblog\node_modules\.bin\redwood' still just opens a js file.
    – Ryan
    Apr 25, 2020 at 23:29
  • Did not work for me either, even after PS restart.
    – Oliver
    Dec 7, 2021 at 12:51
9

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

  1. Open Start > All Programs > Accessories > Windows Powershell.
  2. Right click Windows Powershell shortcut.
  3. Left click "Open file location".
  4. Highlight the location bar on the top.
  5. Right click the location bar.
  6. Left click "Copy".

Part 2: Set .ps1 File Association

  1. Go to the location of your .ps1 file.
  2. Right click the .ps1 file.
  3. Left click "Properties".
  4. Find the part where it said "Opens with:". Left click the "Change..." button on its right side.
  5. Left click the "Browse..." button on the bottom right.
  6. Highlight the location bar on the top.
  7. Right click the location bar.
  8. Left click "Paste".
  9. Left click the button with an arrow pointing right on the right side of the location bar.
  10. Left click "powershell.exe".
  11. Left click "Open"
  12. Go back to the "Open with" window.
  13. Left click "OK" on the bottom right.
  14. Go back to the properties window.
  15. Left click "OK" on the bottom right.

Part 3: Run .\filename.ps1 From Command Prompt

  1. Run this from command prompt.

.\filename.ps1

-Y.P.

0

Several users are facing this problem even with the accepted solution.

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"

This should resolve the problem, if not the problem is in the file extension type, not in the execution policy.

As per PowerShell Module Deployment, difference between *ps1 and *psm1 is:

While the only difference between the two is the extension, it is far more cumbersome to develop directly with the psm1 files as they cannot be directly executed.

I copied the code and saved it in a new file with a *ps1 extension and executed perfectly fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .