How to let *.ps1 scripts be automatically executed by powershell?


  1. Posts : 70
    Windows 10
       #1

    How to let *.ps1 scripts be automatically executed by powershell?


    I am working on 64bit Windows 10 22H2 with installed Powershell v5.1.19041

    When I open now a command prompt and enter a command like

    test.ps1

    then the PS script test.ps1 is NOT executed but an editor pops up showing me the textual content of file test.ps1

    How can I tell Windows Command Prompt to execute all *.ps1 files automatically by powershell?

    Even worse: if I enter (at the command prompt)

    powershell test.ps1

    I am getting an error:

    test.ps1 : The term 'test.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + test.ps1
    + ~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (test.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


    So even if I force Windows 10 to use powershell for executing the *.ps1 script it doesn't work.

    If I enter simply

    powershell

    then powershell interpreter is found and shows:

    Windows PowerShell
    Copyright (C) Microsoft Corporation. All rights reserved.
    Try the new cross-platform PowerShell https://aka.ms/pscore6


    Any other solution?
      My Computer


  2. Posts : 17,039
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    Not
    Code:
    powershell test.ps1
    but, amongst other methods,
    Code:
    powershell -File test.ps1
    if used in a Cmd window.
    And you can also right-click on test.ps1 and select Run with PowerShell


    my ditty - Batch file and PowerShell guides [post #16] - TenForums


    tobwen said:
    When I open now a command prompt and enter a command like
    test.ps1
    ... How can I tell Windows Command Prompt to execute all *.ps1 files automatically by powershell?
    You are trying to use the Cmd window for things that only have meaning to PowerShell windows.
    You could always open a PowerShell windows instead by right-clicking on the Start button & selecting a PowerShell entry [which is there by default].
    In a PS window, you would need to use
    Code:
     .\StartSampleBatch.ps1

    When trying to run any ps1, you'll need to learn about ExecutionPolicy.
    Change PowerShell Script Execution Policy - TenForumsTutorials
    How to enable execution of PowerShell scripts - Super User


    Denis
    Last edited by Try3; 20 Feb 2024 at 09:30.
      My Computer


  3. Posts : 70
    Windows 10
    Thread Starter
       #3

    Ok thank you.

    Code:
    powershell -File test.ps1
    is working.

    Just one more related questions:

    If I double click on a *.ps1 file in Windows Explorer then NO Command prompt is involved.
    So I expected that this *.ps1 file is automatically forwarded to powershell interpreter, but is isn't.

    Similarly to my initial problem when I double click a editor pops up with the *.ps1 file content.

    So how can I execute a *.ps1 file simply by double click?

    or more advanced:

    Assume I have a shortcut on desktop which points to a *.ps1 script file.

    When I drag&drop now say an image file onto this *.ps1 script file for further processing then nothing happens.
    Even not an editor popup.

    How does that work?

    All these "Executions policies" flags and directives are only relevant after powershell detected that it has to execute something.

    So I need at first a logical link between the *.ps1 file extension and powershell
      My Computer


  4. Posts : 17,039
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    tobwen said:
    If I double click on a *.ps1 file in Windows Explorer then NO Command prompt is involved.
    So I expected that this *.ps1 file is automatically forwarded to powershell interpreter, but is isn't.

    Similarly to my initial problem when I double click a editor pops up with the *.ps1 file content.
    That is how Windows is configured by default.
    It might be possible to change that but I have not made any notes on the subject.
    You could search the forum to see if any method has been posted.

    If you read the link I sent you then you'll see there are some introductions to PowerShell in TenForumsTutorials.
    There other, more specialised, tutorials about powershell as well.

    tobwen said:
    So I need at first a logical link between the *.ps1 file extension and powershell
    Use the PowerShell window not the Cmd window or run your ps1 by right-clicking on it in File explorer as I mentioned before.


    Best of luck,
    Denis



    Welcome to TenForums.

    It's really worth making time to browse through the Tutorial index - there's a shortcut to it at the top of every page.
    - At the foot of the Tutorial index is a shortcut to download it as a spreadsheet.
    - I download a new copy each month.
    - By downloading it as a spreadsheet I can benefit from Excel's excellent filtering capabilities when I search for topics of interest.
    - Tutorials are also listed by category at Tutorials - there's also a shortcut to that at the top of every page.
    - Both tutorial lists are searchable.
    - You can also search for TenForumsTutorials in many general search engines, such as Google, by adding site:tenforums.com/tutorials after your search term. For example,
    taskbar toolbars site:tenforums.com/tutorials

    You can search TenForums using the search box in the top-right corner of all TenForums webpages or using Advanced Search - TenForums
    - You can also search TenForums threads in many general search engines, such as Google, by adding site:tenforums.com after your search term. For example,
    Search for drivers by HardwareID site:tenforums.com
    - [This is what the search box in the top-right corner of TenForums webpages does automatically]
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 21:41.
Find Us




Windows 10 Forums