How can I associate a file type with a powershell script?
Asked Answered
R

7

16

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

Param(
  [string]$fileName
) 

echo "Woo I opened $fileName"

When I run it on the command line, it works:

my-script.ps1 .\somefile

Returns:

Woo I opened somefile

I would like to associate my-script.ps1 with a particular file type. I am attempting to do this via 'Open With' However:

  • Windows doesn't include Powershell scripts as 'Programs' (though it considers CMD and batch scripts as 'Programs')

enter image description here

  • When I pick 'All Files' instead, and pick my powershell script, Windows shows this message

enter image description here

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

Ricer answered 16/1, 2018 at 11:42 Comment(2)
This question has had more than 2500 views now - I wonder if @rich-turner feels like bugging the Windows team to add ps1 to the list alongside com and bat. πŸ˜‰ – Ricer
Unfortunately, even though this is tagged with Windows-10, the cmd options no longer work. I learned this after struggling too long. Only XML routes work. I'm not certain the cutoff in Windows releases, but the reference is here (first two sentences): learn.microsoft.com/en-us/archive/blogs/hewagen/… – Cauca
S
21

Use the proper tools for the job:

cmd /c assoc .fob=foobarfile
cmd /c ftype foobarfile=powershell.exe -File `"C:\path\to\your.ps1`" `"%1`"

Note that both assoc and ftype are CMD-builtins, so you need to run them via cmd /c from PowerShell.

For files without extension use this association:

cmd /c assoc .=foobarfile
Swaddle answered 16/1, 2018 at 12:26 Comment(2)
Do you happen to know if assoc and ftype affect current user or local machine? I no longer remember, but I remember that biting me. – Kono
I didn't check for non-admin users, but for a user with admin privileges the commands modify HKLM\Software\Classes. – Swaddle
B
4

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 .<extension>, 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...

Boito answered 16/1, 2018 at 12:14 Comment(2)
I need to create an association with a file that has no extension (to handle launching it correctly). Is this possible? – Ricer
Hrm looks like it: itprotoday.com/management-mobility/… – Ricer
C
4

For those like me who got here looking for general file types associations, I ended up using this function:

Function Create-Association($ext, $exe) {
    $name = cmd /c "assoc $ext 2>NUL"
    if ($name) { # Association already exists: override it
        $name = $name.Split('=')[1]
    } else { # Name doesn't exist: create it
        $name = "$($ext.Replace('.',''))file" # ".log.1" becomes "log1file"
        cmd /c "assoc $ext=$name"
    }
    cmd /c "ftype $name=`"$exe`" `"%1`""
}

I struggled with proper quoting from @Ansgar Wiechers's answer but finally got it right :)

Connecticut answered 26/11, 2019 at 10:35 Comment(2)
You need to update this < cmd /c 'assoc $ext=$name' > into < cmd /c "assoc $ext=$name" > then this will work. :D – Dissever
@MaxBarrass HAAAAAAA :)) – Connecticut
R
2

Here is a concrete answer tested under Windows 7 (but should work under 10 too). Open an administrative command shell and execute the following two lines once:

> assoc .ps1=Microsoft.PowerShellScript.1
> ftype Microsoft.PowerShellScript.1=%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -File "%1"

Now PS1-files are executed by PowerShell 1.0 and not opened by Notepad anymore when double-clicked in the Explorer.

Rodie answered 17/1, 2021 at 15:40 Comment(0)
L
1

Here's my remix of @Matthieu 's great remix of @Ansgar Weichar's great answer.

Matthieu's is set up for executables, and doesn't work for powershell scripts for the same reasons the OP describes.

Function Set-FileAssociationToPowerShellScript($extension, $pathToScript) {

    # first create a filetype
    $filetype = cmd /c "assoc $extension 2>NUL"
    if ($filetype) {
        # Association already exists: override it
        $filetype = $filetype.Split('=')[1]
        Write-Output "Using filetype $filetype"
    }
    else {
        # Name doesn't exist: create it
        # ".log.1" becomes "log1file"
        $filetype = "$($extension.Replace('.', ''))file"
        Write-Output "Creating filetype $filetype ($extension)"
        cmd /c "assoc $extension=$filetype"
    }
    Write-Output "Associating filetype $filetype ($extension) with $pathToScript.."
    cmd /c "ftype $filetype=powershell.exe -File `"$pathToScript`" `"%1`""
}
Lexie answered 6/4, 2020 at 17:24 Comment(0)
B
0

For get associate app i use [!magic] :) :

Set-Alias fa Get-AssocApp

#Get application by file extension Proto1
function Get-AssocApp ([string] $FileType) { 
        ((cmd /c echo ((cmd /c ftype  ( (cmd /c assoc $FileType) -replace "(.*=)*" ,"")) -replace "(.*=)","")).Replace('"','') -replace "\s%+.*$" ,"")

}

Out:

PS C:\Users\user> fa .txt
C:\WINDOWS\system32\NOTEPAD.EXE
PS C:\Users\user> fa .pdf
C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe
PS C:\Users\user> fa .js
C:\Windows\System32\WScript.exe
Bombazine answered 27/3, 2021 at 18:11 Comment(0)
R
0

assoc & ftype no longer works, because file type association is now protected by a secret hash in the registry ... And I can tell You, because I tried it ...

Here is the only solution :

  1. https://kolbi.cz/blog/2017/10/25/setuserfta-userchoice-hash-defeated-set-file-type-associations-per-user/

  2. https://setuserfta.com

Hope this help

Robot answered 18/7 at 13:24 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.