You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/learning-powershell/using-vscode.md
+79-44
Original file line number
Diff line number
Diff line change
@@ -16,46 +16,46 @@ Editing with Visual Studio Code
16
16
17
17
***Linux**: follow the installation instructions on the [Running VS Code on Linux](https://code.visualstudio.com/docs/setup/linux) page
18
18
19
-
***macOS**: follow the installation instructions on the [Running VS Code on macOS](https://code.visualstudio.com/docs/setup/osx) page
19
+
***macOS**: follow the installation instructions on the [Running VS Code on macOS](https://code.visualstudio.com/docs/setup/mac) page
20
20
21
-
**NOTE:** On OS X you must install OpenSSL for the PowerShell extension to work correctly. The easiest way to
22
-
accomplish this is to install [Homebrew](http://brew.sh/) and then run `brew install openssl`. The PowerShell extension
23
-
will now be able to load successfully.
21
+
**NOTE:** On OS X you must install OpenSSL for the PowerShell extension to work correctly.
22
+
The easiest way to accomplish this is to install [Homebrew](http://brew.sh/) and then run `brew install openssl`.
23
+
The PowerShell extension will now be able to load successfully.
24
24
25
25
***Windows**: follow the installation instructions on the [Running VS Code on Windows](https://code.visualstudio.com/docs/setup/windows) page
26
26
27
27
28
28
**2. Installing PowerShell Extension**
29
29
30
-
- Launch the Visual Studio Code app by:
31
-
***Windows**: typing **code** in your PowerShell session
32
-
***Linux**: typing **code .** in your terminal
33
-
***macOS**: typing **code** in your terminal
30
+
- Launch the Visual Studio Code app by:
31
+
* **Windows**: typing **code** in your PowerShell session
32
+
* **Linux**: typing **code** in your terminal
33
+
* **macOS**: typing **code** in your terminal
34
34
35
+
- Launch **Quick Open** by pressing **Ctrl+P** (**Cmd+P** on Mac).
36
+
- In Quick Open, type **ext install powershell** and hit **Enter**.
37
+
- The **Extensions** view will open on the Side Bar. Select the PowerShell extension from Microsoft.
38
+
You will see something like below:
35
39
36
-
- Press **F1** (or **Ctrl+Shift+P**) which opens up the "Command Palette" inside the Visual Studio Code app.
37
-
- In the command palette, type **ext install** and hit **Enter**. It will show all Visual Studio Code extensions available on your system.
38
-
- Choose PowerShell and click on **Install**, you will see something like below
40
+

39
41
40
-

42
+
- Click the **Install** button on the PowerShell extension from Microsoft.
43
+
- After the install, you will see the **Install** button turns to **Reload**.
44
+
Click on **Reload**.
45
+
- After Visual Studio Code has reload, you are ready for editing.
41
46
42
-
- After the install, you will see the **Install** button turns to **Enable**.
43
-
- Click on **Enable** and **OK**
44
-
- Now you are ready for editing.
45
47
For example, to create a new file, click **File->New**.
46
-
To save it, click **File->Save** and then provide a file name, let's say "helloworld.ps1".
48
+
To save it, click **File->Save** and then provide a file name, let's say "HelloWorld.ps1".
47
49
To close the file, click on "x" next to the file name.
48
50
To exit Visual Studio Code, **File->Exit**.
49
51
50
52
#### Using a specific installed version of PowerShell
51
53
52
-
If you wish to use a specific installation of PowerShell with Visual Studio Code,
53
-
you will need to add a new variable to your user settings file.
54
+
If you wish to use a specific installation of PowerShell with Visual Studio Code, you will need to add a new variable to your user settings file.
54
55
55
56
1. Click **File -> Preferences -> User Settings**
56
-
2. Two editor panes will appear. In the right-most pane (`settings.json`), insert the setting below
57
-
appropriate for your OS somewhere between the two curly brackets (`{` and `}`) and replace *<version>*
58
-
with the installed PowerShell version:
57
+
2. Two editor panes will appear.
58
+
In the right-most pane (`settings.json`), insert the setting below appropriate for your OS somewhere between the two curly brackets (`{` and `}`) and replace *<version>* with the installed PowerShell version:
59
59
60
60
```json
61
61
// On Windows:
@@ -73,56 +73,91 @@ you will need to add a new variable to your user settings file.
73
73
74
74
Debugging with Visual Studio Code
75
75
----
76
+
### No-workspace debugging
77
+
As of Visual Studio Code version 1.9 you can debug PowerShell scripts without having to open the folder containing the PowerShell script.
78
+
Simply open the PowerShell script file with **File->Open File...**, set a breakpoint on a line (press F9) and then press F5 to start debugging.
79
+
You will see the Debug actions pane appear which allows you to break into the debugger, step, resume and stop debugging.
80
+
81
+
### Workspace debugging
82
+
Workspace debugging refers to debugging in the context of a folder that you have opened in Visual Studio Code using **Open Folder...** from the **File** menu.
83
+
The folder you open is typically your PowerShell project folder and/or the root of your Git repository.
84
+
85
+
Even in this mode, you can start debugging the currently selected PowerShell script by simply pressing F5.
86
+
However, workspace debugging allows you to define multiple debug configurations other than just debugging the currently open file.
87
+
For instance, you can add a configurations to:
88
+
89
+
* Launch Pester tests in the debugger
90
+
* Launch a specific file with arguments in the debugger
91
+
* Launch an interactive session in the debugger
92
+
* Attach the debugger to a PowerShell host process
93
+
94
+
Follow these steps to create your debug configuration file:
95
+
1. Open the **Debug** view by pressing **Ctrl+Shift+D** (**Cmd+Shift+D** on Mac).
96
+
2. Press the **Configure** gear icon in the toolbar.
97
+
3. Visual Studio Code will prompt you to **Select Environment**.
98
+
Choose **PowerShell**.
76
99
77
-
- Open a file folder (**File->Open Folder**) that contains the PowerShell modules or scripts you have written already and want to debug.
78
-
In this example, we saved the helloworld.ps1 under a directory called "demo".
79
-
Thus we select the "demo" folder and open it in Visual Studio Code.
80
-
81
-
- Creating the Debug Configuration (launch.json)
82
-
83
-
Because some information regarding your scripts is needed for debugger to start executing your script, we need to set up the debug config first.
84
-
This is one-time process to debug PowerShell scripts under your current folder.
85
-
In our case, the "demo" folder.
100
+
When you do this, Visual Studio Code creates a directory and a file ".vscode\launch.json" in the root of your workspace folder.
101
+
This is where your debug configuration is stored. If your files are in a Git repository, you will typically want to commit the launch.json file.
102
+
The contents of the launch.json file are:
86
103
87
-
* Click on the **Debug** icon (or **Ctrl+Shift+D**)
88
-
* Click on the **Settings** icon that looks like a gear.
89
-
Visual Studio Code will prompt you to **Select Environment**.
90
-
Choose **PowerShell**.
91
-
Then Visual Studio Code will auto create a debug configuration settings file in the same folder.
92
-
It looks like the following:
93
104
```json
94
105
{
95
106
"version": "0.2.0",
96
107
"configurations": [
97
108
{
98
-
"name": "PowerShell",
99
109
"type": "PowerShell",
100
110
"request": "launch",
101
-
"program": "${file}",
111
+
"name": "PowerShell Launch (current file)",
112
+
"script": "${file}",
102
113
"args": [],
103
114
"cwd": "${file}"
115
+
},
116
+
{
117
+
"type": "PowerShell",
118
+
"request": "attach",
119
+
"name": "PowerShell Attach to Host Process",
120
+
"processId": "${command.PickPSHostProcess}",
121
+
"runspaceId": 1
122
+
},
123
+
{
124
+
"type": "PowerShell",
125
+
"request": "launch",
126
+
"name": "PowerShell Interactive Session",
127
+
"cwd": "${workspaceRoot}"
104
128
}
105
129
]
106
130
}
131
+
107
132
```
108
-
- Once the debug configuration is established, go to your helloworld.ps1 and set a breakpoint by pressing **F9** on a line you wish to debug.
109
-
- To disable the breakpoint, press **F9** again.
110
-
- Press **F5** to run the script.
111
-
The execution should stop on the line you put the breakpoint on.
112
-
- Press **F5** to continue running the script.
133
+
This represents the common debug scenarios.
134
+
However, when you open this file in the editor, you will see an **Add Configuration...** button.
135
+
You can press this button to add more PowerShell debug configurations. One handy configuration to add is **PowerShell: Launch Script**.
136
+
With this configuration, you can specify a specific file with optional arguments that should be launched whenever you press F5 no matter which file is currently active in the editor.
137
+
138
+
Once the debug configuration is established, you can select which configuration you want to use during a debug session by selecting one from the debug configuration drop-down in the **Debug** view's toolbar.
113
139
114
140
There are a few blogs that may be helpful to get you started using PowerShell extension for Visual Studio Code
115
141
116
142
- Visual Studio Code: [PowerShell Extension][ps-extension]
117
143
-[Write and debug PowerShell scripts in Visual Studio Code][debug]
118
144
-[Debugging Visual Studio Code Guidance][vscode-guide]
119
145
-[Debugging PowerShell in Visual Studio Code][ps-vscode]
120
-
146
+
-[Get started with PowerShell development in Visual Studio Code][getting-started]
147
+
-[Visual Studio Code editing features for PowerShell development – Part 1][editing-part1]
148
+
-[Visual Studio Code editing features for PowerShell development – Part 2][editing-part2]
149
+
-[Debugging PowerShell script in Visual Studio Code – Part 1][debugging-part1]
150
+
-[Debugging PowerShell script in Visual Studio Code – Part 2][debugging-part2]
0 commit comments