15

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?

8
  • I have the same issue. Have you tried this? Commented Oct 18, 2020 at 13:16
  • 3
    See if unchecking "PowerShell: Enable Profile Loading" ("powershell.enableProfileLoading": false) helps. If not, check for any non-Microsoft process hooks that may be injected into your Code.exe and Powershell.exe processes. In my experience, Avecto Privilege Guard hooks (PGHook.dll) are notorious offenders for VS Code PowerShell issues. Commented Oct 18, 2020 at 20:57
  • 1
    I assume waiting a little while does not work, and this happens every time? Just checking as I've found occasionally if I try to run a script before the window finishes loading, it will break for the rest of the session or until I run "restart PowerShell session" from the command palette. Commented Oct 18, 2020 at 21:53
  • 1
    @marsze Reviewing Windows hooks is beyond the scope of this topic, but you can use sysinternals "Process Explorer" to view loaded modules of running Windows processes. If you see a non-Microsoft DLL listed in a process, then it's a good bet that it's a hook. Similarly, the link you presented earlier has no mention of "powershell.enableProfileLoading": false. Sorry. Commented Oct 18, 2020 at 22:31
  • 2
    F5 is the OOBE (out of box experience) VSCode default for debug runs. IT shows this in the Run menu option as the first selection. In the left-hand icon menu that is the debug icon to click for the run. After you install the extension, you still need to configure your settings for the preferences (User and Workspace) you'd want. THe VSCode dos detail this, as well as many videos on Youtube. Just search for 'VSCode PowerShell settings', and 'VSCode PowerShell debug'. There are several 'launch.json' files created on your system in several locations. Search you system for them for review. Commented Oct 18, 2020 at 23:51

5 Answers 5

19

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.

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

1 Comment

I didn't want to edit the post in case I'm missing something... But I'm guessing "command"="ctrl".
6

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}"
}

Comments

5

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

1 Comment

This fixed it for me, ran into an error running the above command, but a different version of it found here helped me. stackoverflow.com/questions/66305351/…
2

Experience the same issue 03/7/2024: Unable to debug after initial breakpoint was hit.

Resolution: Installed the latest version of the PowerShell Extensions for Visual Studio Code (Visual Studio Code 2024.3.1) (Follow Install Instructions)

In my case I downloaded the vsix file (Search 'Download Extension' on MarketPlace Page)

code --install-extension C:\PathTo_VSIX\ms-vscode.PowerShell-2024.3.1.vsix

Comments

1

This error message can pop up 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.