Home > Back-end >  Run Powershell script from anywhere in VScode (basically hotkey a .ps1 script)
Run Powershell script from anywhere in VScode (basically hotkey a .ps1 script)

Time:12-21

I write text files in markdown format, in Visual Studio Code. I have a powershell script (.ps1) that I use to convert a markdown file (.md) to a pdf file.

Currently, I have to go to the .ps1 file to execute every time I want to generate the pdf-file. I would like to run the .ps1 file without leaving the markdown file, preferably with a hotkey (f5?). Can anyone explain how to do this?

CodePudding user response:

You can achieve this by configuring a custom VS code keybinding.

  1. Open keybindings.json document. To do this, use ctrl shift p and then select Preferences: Open Keyboard Shortcuts (JSON).
  2. Create an entry to run your script (see example below).

This example will run the Powershell script C:\Users\paolo\hello-world.ps1 when f5 is pressed:

{
    "key": "f5",
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "powershell C:\\Users\\paolo\\hello-world.ps1\u000D" }
}

although do note that f5 is already configured to run debugging, so I wouldn't use that.

  • Related