How to set file associations in PowerShell so that .ps1 is exec by PS


  1. Posts : 3
    Windows 10 Pro
       #1

    How to set file associations in PowerShell so that .ps1 is exec by PS


    How to set file associations in PowerShell so that a .ps1 Script is executed by PowerShell when double clicked.
    I tried:
    Code:
    cmd /c "ASSOC .ps1=Microsoft.PowerShellScript.1"
    cmd /c "FTYPE Microsoft.PowerShellScript.1=""C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"" ""%1"""
    No error is shown when executed and the desktop is shortly after refreshed, but unfortunately the .ps1 Script is still not executed by PowerShell.

    I noticed under
    Code:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\
    a
    Code:
    UserChoice
    Folder is created when one manually sets a "Open with" program. This folder contains two important entries:
    Code:
    1.) Hash                REG_SZ               wk2qS8kKVnU <- random value automatically created by Windows 
    2.) ProgId               REG_SZ               Applications\notepad.exe
    But unfortunately one can't manually set a Hash since the Hash value is created automatically by Windows, so the value is kinda secret? -- additionally, the value has a different value each time, despite the a same program name.
      My Computer


  2. Posts : 42,634
    Win 10 Pro (22H2) (2nd PC is 22H2)
       #2

    How do you do that?
    - have you tried rt clicking your .ps1 file, Open With, and browse to psexec.exe? "Look for another app"?

    Or you could use (freeware)
    How to set file associations in PowerShell so that .ps1 is exec by PS-1.jpg
      My Computers


  3. Posts : 17,661
    Windows 10 Pro
       #3
      My Computer


  4. Posts : 3
    Windows 10 Pro
    Thread Starter
       #4

    Sorry, I forgot to mention that it would be set through a GPO, so no user interaction required. All the user would do is to double click the script and PowerShell would execute it.
      My Computer


  5. Posts : 17,661
    Windows 10 Pro
       #5

    WinTenUsr said:
    Sorry, I forgot to mention that it would be set through a GPO, so no user interaction required. All the user would do is to double click the script and PowerShell would execute it.
    That can't be done. PowerShell scripts cannot be run by double clicking their icon. The only way is to use shortcut as told in tutorial I linked to.
      My Computer


  6. Posts : 3
    Windows 10 Pro
    Thread Starter
       #6

    Thanks, good or not good to know that this can't be done (up to Win7 it was possible AFAIK). I decided to start the PowerShell script through .cmd/.bat, as these are already set to be executed by CMD.
      My Computer


  7. Posts : 1
    Windows 10 (1809)
       #7

    Kari said:
    That can't be done. PowerShell scripts cannot be run by double clicking their icon. The only way is to use shortcut as told in tutorial I linked to.
    I know this is an old post, but I stumbled across this when my UserChoice value got screwed up and couldn't remember where this was. Thanks for the reg values!

    Also, I get frustrated when people say "it can't be done" with computers. To enable double-clicking of PS1 files in Windows 10:
    1. Remove the UserChoice key [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\UserChoice] if it exists
    2. Change the default Open command for Microsoft.PowerShellScript.1 ([HKLM\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Open\Command\@]) from the existing default to:
    "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File "%1"
    3. Go back to Windows Explorer and double click on a safe PowerShell script to have it run
      My Computer


  8. Posts : 34
    Win10 Home x64 on 3 machines, Win7 Pro x64 on 1.
       #8

    This info is still relevant ... to me!


    Tony4219:

    In Step 1, running XPE [WinPE building project by ChrisR] with PSHell Core (not PSHell) does not have that key.
    He has PS Core installed on Y:\Programs\ not on C: or X:
    In Step 2, key exists and Command is set to default REG_SZ to "X;\Windows\System32\notepad.exe" "%1"
    I will change this to path for PS Core in XPE, which is "Y:\Programs\PowerShell Core_x64\pwsh.exe"
    and add the latter:
    "Y:\Programs\PowerShell Core_x64\pwsh.exe -File "%1""

    Will then export Micrsoft.PowerShellScript.1 key and convert to WinBuilder script w/regCPE. The
    rest of the key will still have references to things on X: but I can now DblClick a .ps1 file and it RUNS!

    After an 1.5 hrs of trying to log into TenForums and getting several PW changes ... Thankx Small Foxx!
      My Computer


  9. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #9

    Hello @Tony4219,

    Is that better suited in this Thread? => Win10XPE - Build Your Own Rescue Media [2]
      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 23:41.
Find Us




Windows 10 Forums