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.
-
I'm also very interested in adding (and running) PowerShell scripts by right-clicking on things. If it's possible, please - do let me know :) – Shark Feb 03 '16 at 13:54
-
1Possible duplicate of [How to start PowerShell from Windows Explorer?](http://stackoverflow.com/q/183901/1630171). – Ansgar Wiechers Feb 03 '16 at 14:05
-
Not quite a duplicate. That article is showing how to open PowerShell by right clicking a folder or background of a folder in Explorer. This question is aimed at running a PowerShell command on files through the right click menu. – Deadly-Bagel Feb 03 '16 at 14:17
-
It could be argued that the question isn't exactly a duplicate. That's why I didn't just close it. However, if you take a closer look at the answer, they *are* essentially showing how to run a command. – Ansgar Wiechers Feb 03 '16 at 14:41
-
They are different registry keys in different formats in different hives. You can't figure out the answer to one by having the other. And is every scripting question essentially asking how to run a command? – Deadly-Bagel Feb 08 '16 at 11:52
2 Answers
HKCR:\*\shell contains what you need. Create a new key with the name of what you want to call it (eg "Rename With Date"), then a subkey called "Command" with a default value of your command.
So for example, HKCR:\*\shell\Rename With Date\Command
where the (Default) value is PowerShell -Command Rename-Item "%1" ((Get-Date).ToShortDateString() + " - %1")
or something should do the trick I think.

- 1,612
- 1
- 9
- 21
-
Actually I doubt this specific command will work because Rename-Item is just expecting the filename in the second paramter, not the whole path so you would need to split the path to get just the filename and extention but I cba it's just an example xP – Deadly-Bagel Feb 03 '16 at 14:15
-
I tried the command in a powershell session and it barfed about the forward slashes returned by the date. So it needs something like `'(get-date).toShortDateString().replace("/", "-") …)` – stib Dec 02 '16 at 00:38
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\`""
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.
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).

- 382,024
- 64
- 607
- 775