Top Ten: Get Started Coding PowerShell in with Visual Studio Code
March 29, 2016
![Top Ten: Get Started Coding PowerShell in with Visual Studio Code Top Ten: Get Started Coding PowerShell in with Visual Studio Code](https://eu-images.contentstack.com/v3/assets/blt07f68461ccd75245/blt15473f82e6a53829/66168e5893fcfc3430de4bbe/VScodeF1_0_3.jpg?width=700&auto=webp&quality=80&disable=upscale)
The first step to using Visual Studio Code with PowerShell is to download Visual Studio Code. It’s completely free and there are versions for Windows 7,8 and 10, Debian, Ubuntu, Fedora and CentOS Linux and the Mac OS X, Yosmite, and El Capitan. You can download Visual Studio Code from here.
After downloading and installing Visual Studio Code the next step is to install the PowerShell extension into Visual Studio Code. By default, Visual Studio Code is a lightweight generic editor. However, it is extensible and there are over a hundred extensions available You can learn more about the extensions at Extending Visual Studio Code. To enable PowerShell support open Visual Studio Code and then press Ctrl-P to open the Command Palette. Then in the Command Palette type install ext PowerShell. And select the PowerShell extension from the dropdown list to install the PowerShell extension.
Visual Studio Code is file and folder based. To perform many of the required PowerShell actions you need to use the Visual Studio Code Explorer to open a folder that contains the PowerShell scripts you want to work with. The Explorer pane will show all of the PowerShell scripts in the folder where you can then click on any of the scripts to open it in the editor.
After the PowerShell extension has been added Visual Studio Code provides a full editing experience for PowerShell with color coded syntax. Unlike ISE or Notepad which uses a white background the default editing surface of Visual Studio Code is dark – much like Visual Studio 2015. That might take a bit of getting used to for some administrators. You can create custom color scheme – even copying ISE if you want. By default, comments are green, variables are blue and cmdlets are white. You can open up to three editing windows for side-by-side editing.
The PowerShell extension also adds full IntelliSense support to Visual Studio Code. As you type the different PowerShell cmdlets the PowerShell extension’s IntelliSense will prompt you with the cmdlet names and parameters.
You can also display the complete cmdlet definitions by simply hovering your mouse over the desired PowerShell cmdlet. With the PowerShell extension installed Visual Studio Code will automatically display all of the different cmdlet definitions.
You can also immediately jump to the definition of a function by highlighting the function and then right clicking and selecting Go to Definition from the context menu or by pressing F12. If you’ve highlighted the function in your code the editor will immediately jump to the definition.
You can also choose to run selected pieces of code in the Visual Studio Code editor by selecting the PowerShell code snippet that you want to run and then pressing F8. The results of the PowerShell code snippet will be displayed in the Output window.
Do you wish ISE’s debugger was more powerful? So do I. Visual Studio Code provides a full featured debugging experience with the ability to set breakpoints, watch variables, inspect variables as well as single step through your code. To start the Visual Studio Code debugger you must first select a folder in Explorer then either click on the debug icon on the left or press Ctrl-Shift-D. You can see variable values, watches, and breakpoints in the sidebar on the left side of the screen. Moving the mouse over a variable shows its value. The debug toolbar at the top allows you to run, step-over, step-into, step-out, restart and stop the debug session.
You can also take advantage of the PowerShell Script Analyzer to check your scripts for potential code defects. The PowerShell Script Analyzer applies a set of built-in or customized rules to the scripts being analyzed. You can get the Power Shell analyzer at Download Script Analyzer from PowerShell Gallery.
You can also take advantage of the PowerShell Script Analyzer to check your scripts for potential code defects. The PowerShell Script Analyzer applies a set of built-in or customized rules to the scripts being analyzed. You can get the Power Shell analyzer at Download Script Analyzer from PowerShell Gallery.
IT Administrators don’t usually keep up with the newest technologies that are released for developers. Visual Studio Code is a great case in point. In case you’ve never heard of it, Visual Studio Code is a lightweight open source editor that was developed by Microsoft and it was first released back in April 2015 at Microsoft’s 2015 Build conference. That much is likely to elicit yawns from most Windows administrators but did you know that Visual Studio Code can be a powerful PowerShell development and debugging tool? More interested now? Most IT administrators use the Integrated Scripting Environment (ISE) to code their PowerShell scripts and while ISE is definitely a handy tool it’s also very limited in its editing and debugging capabilities. ISE lacks much of the functionality that developers have come to expect from more powerful tools like Visual Studio. Visual Studio Code in conjunction with Microsoft’s PowerShell extension can provide a more powerful PowerShell editing and debugging experience.
About the Author(s)
You May Also Like