How can I associate a file type with a powershell script?

后端 未结 5 1158
名媛妹妹
名媛妹妹 2021-02-14 10:10

I have a very powershell script that works perfectly and does:

Param(
  [string]$fileName
) 

echo \"Woo I opened $fileName\"

When I run it on

5条回答
  •  孤街浪徒
    2021-02-14 10:32

    I don't think you can do it through Windows UI.

    The goal here is to associate a type with powershell.exe, arguments to which will be

    1. The powershell script
    2. The target filename

    To do this

    1. Launch Regedit.exe. //disclaimer: you are editing Windows registry. Here be tigers.
    2. Go to HKEY_CLASSES_ROOT (admin access, for all users) or HKEY_CURRENT_USER\SOFTWARE\Classes
    3. Create a key named ., e.g. if you want to associate *.zbs - create a key .zbs
    4. Set it's (default) value to something like zbsfile -- this is a reference linking your extension to a filetype.
    5. Create a key called zbsfile - this is your filetype
    6. Set (default) value to something readable, e.g. "This file is ZBS."
    7. Underneath create a tree of keys (examples are all around):

    zbsfile shell open command

    1. Under command, set (default) value to e.g.
      powershell.exe -File "C:\path\to your\file.ps1" "%1"
      where %1 means the file user clicked

    That should work.

    EDIT: or (crazy idea), create a bat file doing just powershell.exe -File "C:\path\to your\file.ps1" "%%1" and select it in Windows UI...

提交回复
热议问题