27

Using Powershell ISE I often set breakpoints in a script and start the script from command line. ISE then stops at the breakpoint and lets me debug from there. How do I do the same from Terminal in Visual Studio Code? Below is a script just to show you what I mean. Starting from a terminal I would write:

.\hello.ps1 -firstName "firstName" -lastName "theLastName"

But doing that from terminal it just starts a new window.

param (
   [string] $firstName,
   [string] $lastName
)

Write-Host "Starting test script"
try
{
   Write-Host "Hello $firstName $lastName"        
}
catch
{
    Write-Error "Error. Exception: $_"
}

Write-Host "Script done"

2 Answers 2

27

To make the info from Eickhel Mendoza's link explicit - the solution is to set up a Run and Debug config file. This is created in .vscode/launch.json. The content for this case would be:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Hello Script",
            "type": "PowerShell",
            "request": "launch",
            "script": "${workspaceFolder}\\hello.ps1",
            "cwd": "${workspaceFolder}",
            "args": ["-firstName \"firstName\" -lastName \"theLastName\""]
        }
    ]
}

Then just open the "Run and Debug" sidebar pane on VSCode (the play button with the bug or Ctrl+Shift+D), where you can run your launch script and any breakpoints you set will be hit as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

Where does this file live? Under the folder where the ps1 is? I am not using workspace, just plain old ps1 files. Will adding this magical file make it work? Will other pwsh scripts then use that file as well?
I'm not too familiar with the concept of workspaces but afaik the .vscode folder just lives at the root of the folder you have open in VSCode. This If you want to run a different file looks like you just add a new object to the configurations array - more information here
6

Maybe this might help... it involves modifying the launch.json file in your workspace.

https://github.com/PowerShell/vscode-powershell/tree/master/examples#passing-arguments-to-the-script

1 Comment

It is best to post suggestions as comment rather than as answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.