Visual Studio Code won't let me debug PowerShell scripts

Will Pittenger 281 Reputation points
2022-09-04T07:30:03.673+00:00

I'm trying to debug my PowerShell 7 script in Visual Studio Code 1.71.0. I have extension ms-vscode.powershell Version: 2022.8.5 installed. I don't understand why it doesn't come ready to debug PS1 files out of the box, but it doesn't. When I tried it, I was told I had to create a launch.json. Wouldn't that be specified by the extension?

So based on Using Visual Studio Code for PowerShell Development - PowerShell | Microsoft Learn, I added the code below to my settings.json file:

   {  
     "version": "0.2.0",  
     "configurations": [  
         {  
             "type": "PowerShell",  
             "request": "launch",  
             "name": "PowerShell Launch (current file)",  
             "script": "${file}",  
             "args": [],  
             "cwd": "${file}"  
         },  
         {  
             "type": "PowerShell",  
             "request": "attach",  
             "name": "PowerShell Attach to Host Process",  
             "processId": "${command.PickPSHostProcess}",  
             "runspaceId": 1  
         },  
         {  
             "type": "PowerShell",  
             "request": "launch",  
             "name": "PowerShell Interactive Session",  
             "cwd": "${workspaceRoot}"  
         }  
     ]  
   }  

Now I can start debugging? Right? Wrong. It runs my script when I press F5, but when I try to run code declared in that script, my code runs without being debugged. Breakpoints aren't hit.

How can I fix this problem?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Daniel Schneider 6 Reputation points
    2022-10-17T15:15:01.01+00:00

    Just wasted 2 hours trying to fix powershell debugging in VS Code ....
    .... until I remembered that we implemented Constrained Mode a few weeks ago ....

    Seems like debugging is not compatible with constrained mode!

    1 person found this answer helpful.
    0 comments No comments

  2. Limitless Technology 39,336 Reputation points
    2022-09-06T07:38:52.793+00:00

    Hello there,

    This can happen if your PackageManagement module isn't up to date, VSCode picks up on that, displays a pop-up on the lower right of the screen asking if you want to update and doesn't consider Powershell started until it's dismissed.

    Installing the latest version of package management and then restarting either the PowerShell session or VSCode resolved the issue for most of the users who faced the similar issues.

    You also may need to make sure that you don't have this Session exited warning. If you do, click "Restart Current Session", then try pressing F5 again.

    I hope this information helps. If you have any questions please let me know and I will be glad to help you out.

    ---------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--


  3. Rich Matheisen 44,621 Reputation points
    2022-09-06T19:14:11.76+00:00

    Is VS Code starting the right version of PowerShell in this session?

    using-vscode