Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot debug PowerShell scripts in Visual Studio Code

I am to run PowerShell scripts in Visual Studio Code with F5.

My Visual Studio Code version is 1.50.1 with commit d2e414d9e42

I get the following error message:

Cannot debug or run a PowerShell script until the PowerShell session has started. Wait for the PowerShell session to finish starting and try again.

I installed the official Microsoft PowerShell extension and my launch.json includes the following:

{
        "name": "PowerShell: Launch Script",
        "type": "PowerShell",
        "request": "launch",
        "script": "countcharacters.ps1",
        "cwd": "${workspaceFolder}"
}

How can I make debugging work with F5?

like image 643
Gergely Avatar asked Oct 18 '20 13:10

Gergely


People also ask

How do I enable Debug mode in PowerShell script?

Press F5 or, on the toolbar, click the Run Script icon, or on the Debug menu, click Run/Continue or, in the Console Pane, type C and then press ENTER . This causes the script to continue running to the next breakpoint or to the end of the script if no further breakpoints are encountered.

How do I run a PowerShell script from Visual Studio code?

Once it is saved as a PS1, VS Code will identify the file as a PowerShell script. From there, you can execute the PowerShell script by press F5 . You can also click the Run button on the top right of the editor. To run a select, you can press F8 or right click on the selection and click the Run Selection option.

Can we Debug PowerShell script?

You can use the features of the PowerShell debugger to examine a PowerShell script, function, command, or expression while it is running. The PowerShell debugger includes a set of cmdlets that let you set breakpoints, manage breakpoints, and view the call stack.


3 Answers

Installing the latest version of package management and then restarting either the PowerShell session or VSCode resolved the issue.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber -Repository PSGallery
like image 83
Dejulia489 Avatar answered Sep 25 '22 23:09

Dejulia489


To solve this problem, you can try to restart your current PowerShell session by:

  1. Opening the command pallet (command+shift+p)
  2. Searching for Restart Current Session and selecting it
  3. Pressing the F5 button again

You may see the issue pop up once more, but just press F5 again and you should see things start to execute.

like image 16
Justin Avatar answered Oct 09 '22 01:10

Justin


Closing all open PowerShell windows before pressing F5 fixed this issue for me.

You also may need to make sure that you don't have this Session exited warning:

Session exited warning

If you do, click "Restart Current Session", then try pressing F5 again. Restart Current Session option

This is with the following config in launch.json (On a Windows 10 machine):

{
    "name": "PowerShell: Launch script.ps1",
    "type": "PowerShell",
    "request": "launch",
    "script": ".\\script.ps1",
    "cwd": "${workspaceFolder}"
}
like image 5
davekats Avatar answered Oct 09 '22 00:10

davekats