Windows Powershell: Executing commands via the context menu?

2

6

I'm using kramdown to convert my markdown-formatted source files into html. The command I've been using is:

kramdown %filename | Out-Clipboard

(Out-Clipboard is from the fantastic PSCX. This means my formatted html is immediately ready to paste into another program or editor)

I'd love to streamline this process and be able to use a context menu item to do the same thing, instead of opening a shell and typing out the command each time. What's the best way to pass these instructions to powershell, and add it as menu option?

eden

Posted 2011-08-11T16:10:38.893

Reputation: 38

Answers

6

You can run PowerShell commands from the command-line (batch files, etc.) by using this format:

C:\> PowerShell <PowerShell command you want to run>

ie: C:\> PowerShell "kramdown MyFileName.txt | Out-Clipboard"

So we can use that in a context menu entry.

For simplicity here's how you can add a new Context Menu command to run it against whichever file you have selected/right-clicked (this is probably not the ONLY way to do this):

  • Head to HKEY_CLASSES_ROOT\*\shell in the registry.
  • Create a new Key named what you want (like say "Kramdown").
  • Set the "Default" REG_SZ value of that new key to the text you want to appear on the context menu (like say "Kram This Down").
  • Create another new Key inside the key you just made, and name it "command".
  • Set the value of the "Default" REG_SZ in that new 'command' key to run the command you want.

The command you'll want to enter is like above, only we need to tell it to do it in the command prompt, and use the %1 variable so it knows the file you clicked on:

cmd /C PowerShell "kramdown %1 | Out-Clipboard"

The /C causes the CMD window to close after it's done.

As soon as you make these registry changes they will affect the context menu, so you don't need to logout or reboot to enable or test your changes.

Since I don't have your source files, Kramdown, or Out-Clipboard I can't test this 100%; but this should be enough info to get you going, if it doesn't 'just work' as-is. :)

Ƭᴇcʜιᴇ007

Posted 2011-08-11T16:10:38.893

Reputation: 103 763

How to get the folder path? – Vijay Nirmal – 2019-09-25T08:45:21.177

@VijayNirmal Which folder path are you referring to? – Ƭᴇcʜιᴇ007 – 2019-09-25T16:29:30.940

Never Mind, I found the solution. Thanks – Vijay Nirmal – 2019-09-26T05:11:18.247