Does Visual Studio Code work with PowerShell in the Command Palette?

10,383

Solution 1

An "Integrated Terminal" is now integrated into Visual Studio Code. This is a PowerShell Terminal in Windows. (Likely BASH in Linux / MacOS). The terminal appears in a panel at the bottom of the code editor, not in the Command Pallete.

Keyboard Shortcut to open Terminal: CTRL + `

Sources:

I confirmed in version 1.19.2. Not sure when the feature was first integrated.

Solution 2

This was the first problem I wanted to solve in VSCode. I did not find a way to type PowerShell commands but I have found a way to create and run PowerShell tasks.

In the command palette type and choose Tasks: Configure Task Runner. It will show you a json file configured for the default task runner.

I replaced the content with this

{
    "version": "0.1.0",
    "command": "PowerShell.exe",
    "isShellCommand": true,
    "args": [
        "-NoProfile",
        "Invoke-Build.ps1"
    ],
    "tasks": [
        {
            "taskName": "Build",
            "isBuildCommand": true,
            "showOutput": "always"
        },
        {
            "taskName": "Test",
            "isTestCommand": true,
            "showOutput": "always"
        },
        {
            "taskName": "Build, Test",
            "showOutput": "always"
        }
    ]
}

As a result in the command palette I can choose and run my predefined tasks (Build, Test, and combined Build, Test). I can add other tasks and probably bind them to some hotkeys. This is not exactly what I would like to have in VSCode for PowerShell but for the preview it is at least something.

P.S. This just my first experiment that somewhat worked. It is not perfect, more likely. There are many configuration parameters for this json file that I have not tried yet.

Solution 3

With version 0.10.1 of Visual Studio Code, you can run and debug PowerShell. Here is a sample launch.json that I use:

{
    "version": "0.2.0",
     "configurations": [ 
        { 
            "name": "PowerShell", 
            "type": "PowerShell", 
            "program": "MyScript.ps1",
            "args": "-Verbose"
        } 
    ] 
}

Unfortunately, I cannot get the args to work (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/24). Another problem I have is that Read-Host does not work with VS Code (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/29). Definitely some rough edges.

Solution 4

Here's how you can configure the Powershell task to execute the currently opened .ps1 file without any Invoke-Build dependency:

{
    "version": "0.1.0",
    "command": "PowerShell.exe",
    "isShellCommand": true,
    "args": [
        "${file}"
    ],
    "tasks": [
        {
            "taskName": "Build",
            "isBuildCommand": true,
            "showOutput": "always"
        },
        {
            "taskName": "Test",
            "isTestCommand": true,
            "showOutput": "always"
        },
        {
            "taskName": "Build, Test",
            "showOutput": "always"
        }
    ]
}

Note: This is just a slight modification of Roman's answer (My edit to his answer was rejected).

Solution 5

Open the Debug view, in the View Bar select Debug from the View menu or press Ctrl + Shift + D.

In the Launch Configuration dropdown (shown in the following screenshot), select Add Configuration...

enter image description here

The launch.json configuration will open in the editor, type PowerShell and select the debug configurations you want, as shown in the following screenshot.

enter image description here

Save the launch.json file and select the debug configuration you want from the Launch Configuration dropdown:

enter image description here

Now you can debug PowerShell scripts through VSCode.

The Scripting Guys wrote a comprehensive 2 part blog post on this topic, like everything else they write, it's well worth a read if you're new to VSCode and PowerShell.

https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/06/debugging-powershell-script-in-visual-studio-code-part-1/

https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/13/debugging-powershell-script-in-visual-studio-code-part-2/

EDIT: I am aware that this question is several years old, but I came across it before I found any up to date information on how to set up PowerShell debugging in VSCode

Share:
10,383

Related videos on Youtube

Dan Sorensen
Author by

Dan Sorensen

I am a full stack web developer primarily focused on the ASP.NET stack with and SQL Server. I like building fast, responsive pages and SQL Server development.

Updated on June 04, 2022

Comments

  • Dan Sorensen
    Dan Sorensen almost 2 years

    With Visual Studio Code (not the traditional Visual Studio IDEs), can you run PowerShell in the Command Palette? I ask because I commonly use it in the full IDE.

    I have not seen PowerShell mentioned in the documentation, other than for basic syntax highlighting. I have tried it with no success. Is it unsupported, or is it an optional feature I can configure somehow?

    Note: to those voting up the PowerGUI answer, it is not correct as it references the wrong edition of Visual Studio for this question. It is helpful if you are using the full IDE, but not the new editor named: Code.

  • Dan Sorensen
    Dan Sorensen almost 9 years
    Great answer and useful in the full IDE (which I use often), but this question is specifically about the new code editor Visual Studio Code.
  • SantanuMajumdar
    SantanuMajumdar almost 9 years
    @DanSorensen Oh Yes, Seems like I missed your edited question.
  • Dan Sorensen
    Dan Sorensen almost 9 years
    Great find! I'll try it out.
  • Roman Kuzmin
    Roman Kuzmin almost 9 years
    NB. This example uses Invoke-Build as the task runner. But you can use your own script with the first parameter [string[]]$Task and process tasks passed in this script.
  • kDar
    kDar almost 9 years
    if you're getting hit by “execution of scripts is disabled on this system” make sure you also enabled script execution on 32 bit Powershell (can be found here: %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe)‌​. Run "Set-ExecutionPolicy RemoteSigned"
  • TJB
    TJB almost 9 years
    When I run this it complains about the "Build" or "Test" argument, is there something else I need to change?
  • Naeem Sarfraz
    Naeem Sarfraz over 8 years
    @TJB did you find a solution to your problem?
  • TJB
    TJB over 8 years
    @NaeemSarfraz No, unfortunately, I went with the "PowerShell for Visual Studio" extension visualstudiogallery.msdn.microsoft.com/…
  • Naeem Sarfraz
    Naeem Sarfraz over 8 years
    @TJB, FYI I found a solution, the code team added a property "suppressTaskName": true which will omit this argument