如何将文件类型与 powershell 脚本相关联? [英] How can I associate a file type with a powershell script?

查看:56
本文介绍了如何将文件类型与 powershell 脚本相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常强大的脚本,可以完美运行:

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

返回:

Woo I opened somefile

我想将 my-script.ps1 与特定文件类型相关联.我试图通过打开方式"来做到这一点:

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

  • Windows 不将 Powershell 脚本包含为程序"(尽管它将 CMD 和批处理脚本视为程序")

  • 当我选择所有文件"并选择我的 powershell 脚本时,Windows 会显示此消息

如何将文件类型与 Powershell 脚本相关联?

推荐答案

为工作使用合适的工具:

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`"

请注意,assocftype 是 CMD 内置的,所以你需要通过 PowerShell 中的 cmd/c 运行它们.

对于没有扩展名的文件,使用此关联:

For files without extension use this association:

cmd /c assoc .=foobarfile

这篇关于如何将文件类型与 powershell 脚本相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆