none
ps1 file opens with Notepad and won’t run

    Question

  • PowerShell Documentation states that I only need to double-click PowerShell script (extension “ps1”) in order to run it.
    This does not happen; instead Notepad opens ps1 script for editing. Set ExecutionPolicy to “RemoteSigned” to no avail.


    Is documentation not specific or is there another problem on my PC (Vista Ultimate x86)?


    Thank you.

    Friday, January 09, 2009 2:33 PM

Answers

  • Sorry, that's not really enough details, and I'm not sure I follow...

    Try this:
    1. Put this in testing.ps1 using Notepad:
    write-host "hello world"

    2. Assuming your current directory in your PowerShell console is the same as where you created testing.ps1, you would simply do:
    PS> ./testing.ps1

    If you get an error about the execution policy, which you should if you've not changed it, run:
    PS> set-executionpolicy remotesigned

    Then, rerun:
    PS> ./testing.ps1
    • Marked as answer by Eug723 Friday, January 09, 2009 7:45 PM
    Friday, January 09, 2009 7:17 PM
    Moderator
  • When you have spaces, things get a bit more complicated:
    PS> & 'c:\my scripts\script.ps1'
    • Marked as answer by Eug723 Friday, January 09, 2009 8:03 PM
    Friday, January 09, 2009 7:51 PM
    Moderator

All replies

  • Can you specify where you see this exactly where the docs mention that you can double-click a .ps1 file?

    This is incorrect.  By default (anyways), you cannot double-click .ps1 scripts and have PowerShell run them.

    This is one of the basic security features that PowerShell provides out of the box.

    Seel also:
    http://blogs.msdn.com/powershell/archive/2006/08/03/687838.aspx
    • Edited by Marco ShawModerator Friday, January 09, 2009 2:38 PM Fixed URL
    • Proposed as answer by Marco ShawModerator Friday, January 09, 2009 2:38 PM
    • Marked as answer by Eug723 Friday, January 09, 2009 2:55 PM
    • Unmarked as answer by Eug723 Friday, January 09, 2009 6:23 PM
    Friday, January 09, 2009 2:37 PM
    Moderator
  • File "GettingStarted.rtf" located in "C:\Windows\System32\WindowsPowerShell\v1.0\Documents\en-US" page 11:

    In addition to its interactive interface, Windows PowerShell fully supports scripting. In Windows PowerShell, script files have a .ps1 file name extension. To run a script, type the name of the script at the command prompt.

    Is this DOS command prompt or PowerShell command prompt? Former one opens Notepad, latter one does nothing.

    Thank you for your reply and link.
    Friday, January 09, 2009 2:55 PM
  • This would be referring to doing something like this:

    PS> ./my_file.ps1

    I do agree that the wording of the doc can be misleading.

    Yes, if you do Start->Run>my_file.ps1, this will open up the script in Notepad.

    You'll see this behaviour on all OSs running PowerShell v1 and the preview versions of v2 also...
    Friday, January 09, 2009 2:59 PM
    Moderator
  •  OK, I copied a script sample from here –

    http://www.microsoft.com/technet/scriptcenter/scripts/msh/default.mspx?mfr=true

    pasted into “sample.ps1” file, and entered its path/name at the PowerShell command line.

    Although I had expected a customized host window (Write-Host creates a customized host window) in reality nothing happened; PowerShell echoed path/name I entered, then PS> prompt re-appeared.

    How do I run PowerShell scripts anyway?

    Please advise.

    Thank you.

    Friday, January 09, 2009 6:23 PM
  • Sorry, that's not really enough details, and I'm not sure I follow...

    Try this:
    1. Put this in testing.ps1 using Notepad:
    write-host "hello world"

    2. Assuming your current directory in your PowerShell console is the same as where you created testing.ps1, you would simply do:
    PS> ./testing.ps1

    If you get an error about the execution policy, which you should if you've not changed it, run:
    PS> set-executionpolicy remotesigned

    Then, rerun:
    PS> ./testing.ps1
    • Marked as answer by Eug723 Friday, January 09, 2009 7:45 PM
    Friday, January 09, 2009 7:17 PM
    Moderator
  • Thank you, this worked.

    However, when I tried full path (i. e. "C:\My Projects\PS Scripts\testing.ps1") it did not.

    Am I not specifying full path correctly or full path can't be used? There is an example of running a script by passing its full path on page 11 of "GettingStarted.rtf"...

    Thanks again.
    Friday, January 09, 2009 7:45 PM
  • When you have spaces, things get a bit more complicated:
    PS> & 'c:\my scripts\script.ps1'
    • Marked as answer by Eug723 Friday, January 09, 2009 8:03 PM
    Friday, January 09, 2009 7:51 PM
    Moderator