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')
- When I pick 'All Files' instead, and pick my powershell script, Windows shows this message
How can I associate a file type with a Powershell script?
For those like me who got here looking for general file types associations, I ended up using this function:
I struggled with proper quoting from @Ansgar Wiechers's answer but finally got it right :)
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.
Use the proper tools for the job:
Note that both
assoc
andftype
are CMD-builtins, so you need to run them viacmd /c
from PowerShell.For files without extension use this association:
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
To do this
Regedit.exe
. //disclaimer: you are editing Windows registry. Here be tigers..<extension>
, e.g. if you want to associate *.zbs - create a key.zbs
zbsfile
-- this is a reference linking your extension to a filetype.zbsfile
- this is your filetypezbsfile shell open command
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...