是否有可能在尝试执行文件时覆盖powershell的行为?

时间:2014-07-23 15:56:37

标签: powershell

当您尝试在powershell中执行文件时,即.\file,它似乎通过Invoke-Item cmdlet执行它。是否可以在我的powershell配置文件中覆盖,替换或扩充此功能?我希望能够通过扩展/用户提示替换默认打开行为的检查,默认情况下更智能。

1 个答案:

答案 0 :(得分:0)

在Windows中解决类似问题的规范方法是将有问题类型的默认操作更改为实现您希望应用于该类型文件的逻辑的自定义脚本/程序。

通过添加更改一些注册表项,可以轻松地更改操作。 zip文件示例:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\CompressedFolder\shell]
;change default action
@="Open2"

[HKEY_CLASSES_ROOT\CompressedFolder\shell\Open2]
;change label of this action
@="My Custom Action"

[HKEY_CLASSES_ROOT\CompressedFolder\shell\Open2\command]
;command for this action
@="powershell.exe -File \"C:\path\to\zip-handler.ps1\" \"%1\""

脚本可以(例如)看起来像这样:

$zip = Get-Item $args[0]

if ($zip.Length -le 10MB) {
  [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
  $targetFolder = Split-Path -Parent $zip.FullName
  [IO.Compression.ZipFile]::ExtractToDirectory($zip.FullName, $targetFolder)
} else {
  explorer.exe $zip.FullName
}