在资源管理器中的选定文件上执行PowerShell命令 [英] Execute PowerShell command on selected files in Explorer

查看:113
本文介绍了在资源管理器中的选定文件上执行PowerShell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PowerShell中仅对资源管理器中的选定文件执行重命名命令?我有PS命令,但是我不知道将它放在Windows注册表中的什么位置,以便在资源管理器的右键单击上下文菜单中可以访问.

How to execute a rename command in PowerShell only on selected files in Explorer? I have the PS command but I don't know where to put it in the Windows registry to be accessible in the right-click context menu in Explorer.

推荐答案

使用Powershell的

It is fairly easy to add a context-menu command to selected files in File Explorer using Powershell's New-Item cmdlet to write a command line to the registry:

# Add a context-menu item named 'Foo' to the context menu of files 
# that executes a PowerShell command that - in this simple demonstration - 
# simply *echoes* the file path given, using Write-Output.
$null = New-Item -Force "HKCU:\Software\Classes\*\shell\Foo\command" | 
        New-ItemProperty -Name '(Default)' -Value `
         "$PSHOME\powershell.exe -NoProfile -NoExit -Command Write-Output \`"%1\`""

以上内容针对HKCU:\Software\Classes\*\shell(HKEY_CURRENT_USER\Software\Classes\*\shell),因此仅适用于文件;仅文件夹位置为HKCU:\Software\Classes\Folder\shell 文件和文件夹的位置均为HKCU:\Software\Classes\AllFileSystemObjects\shell.
还要注意上面的-LiteralPath的使用,这对于防止将HKCU:\Software\Classes\*中的*解释为通配符至关重要.
请注意,写入等效的HKEY_LOCAL_MACHINE键以使该命令对所有用户可用,将需要 elevation (以管理员身份运行).

The above targets HKCU:\Software\Classes\*\shell (HKEY_CURRENT_USER\Software\Classes\*\shell) and therefore applies to files only; the folder-only location is HKCU:\Software\Classes\Folder\shell, and the location for both files and folders is HKCU:\Software\Classes\AllFileSystemObjects\shell.
Also note the use of -LiteralPath above, which is crucial to prevent interpretation of the * in HKCU:\Software\Classes\* as a wildcard.
Note that writing to the equivalent HKEY_LOCAL_MACHINE keys so as to make the command available to all users would require elevation (running as administrator).

是通过这种方法,对于每个选定文件 ,该命令将被多个文件调用被选中.

The caveat is that with this approach the command is invoked for each selected file, should multiple files be selected.

换句话说:如果您打算重命名文件的,并且您的重命名逻辑要求将该组视为整体 ,则上述方法将不会工作.

In other words: if your intent is to rename a group of files and your renaming logic requires that the group be considered as a whole, the above approach won't work.

您可以尝试在脚本本身中解决该问题,但这并不容易.

You could try to work around that in the script itself, but that would be nontrivial.

另一种健壮的解决方案是创建上下文菜单处理程序,该方法需要编写正在实现为DLL的进程内组件对象模型(COM)对象 "(来自文档).

A more robust solution is to create context-menu handlers, which requires writing "in-process Component Object Model (COM) objects implemented as DLLs" (from the docs).

这篇关于在资源管理器中的选定文件上执行PowerShell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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