{"id":80915,"date":"2016-12-05T00:01:24","date_gmt":"2016-12-05T08:01:24","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80915"},"modified":"2019-02-18T09:10:19","modified_gmt":"2019-02-18T16:10:19","slug":"get-started-with-powershell-development-in-visual-studio-code","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/get-started-with-powershell-development-in-visual-studio-code\/","title":{"rendered":"Get started with PowerShell development in Visual Studio Code"},"content":{"rendered":"<p><strong>Summary:<\/strong> Learn about the features in Visual Studio Code that help you write PowerShell code.<\/p>\n<p>Since the PowerShell integrated scripting environment (ISE) shipped with Windows PowerShell 2.0 in 2009, the PowerShell landscape has changed significantly. In November 2014, Microsoft took .NET open source and cross-platform. Given PowerShell\u2019s dependence on .NET, this was an important prerequisite before taking PowerShell cross-platform. And, indeed, nine months later, Microsoft made PowerShell available cross-platform, supporting Linux and macOS in addition to Windows. To the surprise of many, Microsoft also open-sourced PowerShell. Did I mention that Visual Studio Code is also <a target=\"_blank\" href=\"https:\/\/github.com\/microsoft\/vscode\">open source on GitHub<\/a>? Detecting a trend here?<\/p>\n<p>To support cross-platform PowerShell script development, Microsoft started two new open-source projects. The <a target=\"_blank\" href=\"https:\/\/github.com\/powershell\/powershelleditorservices\">PowerShell Editor Services<\/a> project provides language and debug services for editors that want to support PowerShell script development beyond basic syntax colorization. In this regard, PowerShell Editor Services is similar to <a target=\"_blank\" href=\"http:\/\/www.omnisharp.net\/\">OmniSharp<\/a>, which provides C# language support to editors like Atom, Sublime, Vim, and Visual Studio Code. The other project is <a target=\"_blank\" href=\"https:\/\/github.com\/PowerShell\/vscode-powershell\">vscode-powershell<\/a>, which is a Visual Studio Code <a target=\"_blank\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.PowerShell\">extension<\/a> that integrates the PowerShell Editor Services. Both projects have taken significant community contributions.<\/p>\n<h2>Choose a PowerShell scripting environment<\/h2>\n<p>Before diving into the details about how to get started with Visual Studio Code, it is worth mentioning why you might want to use Visual Studio Code beyond the obvious need for Linux or macOS support.\u00a0 Here are a few reasons why you might want to consider using Visual Studio Code:<\/p>\n<ul>\n<li>Enhanced editor services\n<ul>\n<li>Open folder for editing (easily access multiple files)<\/li>\n<li>Peek\/Goto definition<\/li>\n<li>Find all references<\/li>\n<li>Search and replace across multiple files<\/li>\n<li>Go to Symbol<\/li>\n<li>Task runner (configure to run Pester tests)<\/li>\n<li>Toggle render whitespace<\/li>\n<li>Multiple cursor support (edit multiple lines at once)<\/li>\n<li>Background script analysis by using PSScriptAnalyzer with code fix support coming soon<\/li>\n<\/ul>\n<\/li>\n<li>Enhanced debugging\n<ul>\n<li>Variables window with support for setting variables during debug<\/li>\n<li>Watch window for watching variable values<\/li>\n<li>Breakpoints window with support for line, function, and conditional breakpoints<\/li>\n<li>Breakpoint hit count support<\/li>\n<li>Call Stack window<\/li>\n<\/ul>\n<\/li>\n<li>Built-in support for Git version control<\/li>\n<li>Support for multiple color schemes including dark and light schemes<\/li>\n<li>Support for other file types: Batch, CSS, Dockerfile, Html, Ini, JavaScript, JSON, Markdown, Perl, Python, SQL, YAML, etc.<\/li>\n<li>Rich extension ecosystem\n<ul>\n<li><a target=\"_blank\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-mssql.mssql\">mssql<\/a>, <a target=\"_blank\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=PeterJausovec.vscode-docker\">Docker Support<\/a>, <a target=\"_blank\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=donjayamanne.githistory\">Git History<\/a> and over a thousand other extensions<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>On the other hand, if PowerShell ISE meets your needs, then by all means continue to use it. It\u2019s not going anywhere. In fact, there are still a few things that PowerShell ISE can do that the PowerShell extension for Visual Studio Code can\u2019t do \u2013 yet.\u00a0 Here are the major features unique to PowerShell ISE:<\/p>\n<ul>\n<li>Remote runspace tabs<\/li>\n<li>Remote debugging<\/li>\n<li>Language aware code folding<\/li>\n<li>Interactive PowerShell console available during edit and debug<\/li>\n<\/ul>\n<h2>Install and configure Visual Studio Code<\/h2>\n<p>To install Visual Studio Code for your operating system, open <a target=\"_blank\" href=\"http:\/\/code.visualstudio.com\">http:\/\/code.visualstudio.com<\/a> in a browser and follow the instructions. Even without the PowerShell extension installed, you will get syntax colorization and basic autocompletion for PowerShell scripts.<\/p>\n<p>Syntax colorization is specified via a TextMate tmLanguage file. PowerShell syntax colorization is currently provided by the <a target=\"_blank\" href=\"https:\/\/github.com\/PowerShell\/EditorSyntax\">PowerShell Editor Syntax project<\/a> on GitHub. If you find an issue with PowerShell syntax colorization, be sure to submit the issue to that project.<\/p>\n<p>To get the full PowerShell script development experience, let\u2019s install the PowerShell extension for Visual Studio Code. First, select <strong>Extensions<\/strong> from the <strong>View<\/strong> menu (or press Ctrl+Shift+X on Windows). In the <strong>Extensions<\/strong> view, type <em>PowerShell<\/em> in the search box, and then press Enter. The PowerShell extension from Microsoft should show up at the top of the search results.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-120516.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-120516.png\" alt=\"Screenshot of PowerShell extension in search results\" width=\"579\" height=\"558\" class=\"alignnone size-full wp-image-80916\" \/><\/a><\/p>\n<p>Click the <strong>Install<\/strong> button. After the PowerShell extension is installed, you will need to reload Visual Studio Code to use the extension. As an alternative, you can click the <strong>Reload<\/strong> button that appears where the <strong>Install<\/strong> button used to be.<\/p>\n<p>On Windows, you are now set to develop PowerShell scripts in Visual Studio Code. However, on Linux and macOS, you will need to install PowerShell. You can find PowerShell <a target=\"_blank\" href=\"https:\/\/github.com\/PowerShell\/PowerShell\/releases\">releases for all platforms on GitHub<\/a> and <a target=\"_blank\" href=\"https:\/\/github.com\/PowerShell\/PowerShell\/blob\/master\/docs\/installation\/linux.md\">instructions to install PowerShell on these platforms<\/a>.<\/p>\n<p>At this point, you should be able to open PowerShell scripts or folders that contain multiple PowerShell scripts and edit them with rich IntelliSense support and enhanced editor features. You can also start with a new, empty file (called \u201cUntitled-1\u201d). You can tell PowerShell to treat that file as a PowerShell script by either saving the file with a .ps1 extension or by setting the language mode to PowerShell. To set the language mode, open the <strong>Command Palette<\/strong> from the <strong>View<\/strong> menu, or press Ctrl+Shift+P (Cmd+P on Mac).\u00a0 Type <em>change language mode<\/em> or use fuzzy matching by typing <em>clm<\/em>, \u00a0andthen press Enter. After you\u2019re in <strong>Select Language Mode<\/strong>, type <em>ps<\/em> to select <strong>PowerShell<\/strong>, and then press Enter.<\/p>\n<h2>Visual Studio Code basics<\/h2>\n<p>The basic layout of Visual Studio Code is shown in the following screenshot:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-120516.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-120516.png\" alt=\"Basic layout of Visual Studio Code\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-80925\" \/><\/a><\/p>\n<p>The View Bar (A) lets you to quickly switch between the Explorer, Search, Git, Debug, and Extensions views.\u00a0 The Side\u00a0Bar (B) is collapsible (Ctrl+B \/ Cmd+B on macOS), so you can maximize the space that\u2019s available for editing. Editor Groups (C) supports up to three side-by-side groups of editor windows. Editor Groups can be arranged both horizontally, as shown in the screenshot, and vertically. The Panel area (D) displays the Debug Console, Output, Problems, and Integrated Terminal. The Output window displays output from Extensions, Git, and Tasks. The Status Bar (E) displays useful information such as which Git branch is active, the repository status of the active branch, the number of problems and warnings, the cursor\u2019s position, the current file\u2019s encoding and line-endings, as well as the selected language mode for the current file. \u00a0You can even provide feedback to the Visual Studio Code team via the smiley face in the status bar. For a more in-depth getting started guide, see <a target=\"_blank\" href=\"https:\/\/code.visualstudio.com\/docs\/editor\/codebasics\">The Basics of Visual Studio Code<\/a>.<\/p>\n<h2>Customize Visual Studio Code settings<\/h2>\n<p>One of the great features of Visual Studio Code is the extent to which you can customize it. Each extension usually provides customizable settings, too. To begin to customize Visual Studio Code, select the <strong>Command Palette<\/strong> from the <strong>View<\/strong> menu, or press Ctrl+Shift+P (Cmd + P on the Mac), type <em>user<\/em>, and then select <strong>Preferences: Open User Settings<\/strong>. This will open two editor windows as shown in the following screenshot:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-120516.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-120516.png\" alt=\"Two editor windows in Visual Studio Code\" width=\"599\" height=\"458\" class=\"alignnone size-full wp-image-80935\" \/><\/a><\/p>\n<p>As you can see in the <strong>Default Settings<\/strong> tab of the editor on the left, lots of areas of Visual Studio Code can be customized, including extensions like PowerShell. The editor on the left provides the \u201cdefault\u201d settings and is read-only. What you typically do is find a setting in this window that you want to customize, and copy it to the user settings file on the right. The screenshot also demonstrates the \u201cLight+\u201d color scheme.<\/p>\n<p>Following is a basic set of customizations that I would recommend for PowerShell development:<\/p>\n<p style=\"padding-left: 30px\"><code>\/\/ Place your settings in this file to overwrite the default settings\n{<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>\"editor.rulers\": [ 120 ],<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>\"files.trimTrailingWhitespace\": true,<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>\"terminal.integrated.shell.windows\":\n\"C:\\\\WINDOWS\\\\sysnative\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe\"<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>}<\/code><\/p>\n<p>The <strong>editor ruler<\/strong> provides a visual reminder of where you might want to wrap your PowerShell script. The <strong>trimTrailingWhitespace<\/strong> setting is particularly convenient for PowerShell. If you use <strong>`<\/strong> for line continuation, you\u2019ll never have to worry about inadvertent whitespace after a <strong>`<\/strong>. Finally, you will likely want Visual Studio Code\u2019s Integrated Terminal to open PowerShell instead of CMD.exe.<\/p>\n<p>The PowerShell extension comes with the following customizable settings:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-120516.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-120516.png\" alt=\"Customizable settings for PowerShell extension\" width=\"615\" height=\"354\" class=\"alignnone size-full wp-image-80945\" \/><\/a><\/p>\n<p>You can get more in-depth coverage of user settings in the <a target=\"_blank\" href=\"http:\/\/code.visualstudio.com\/docs\/customization\/userandworkspace\">User and Workspace Settings<\/a> article in the Visual Studio Code docs.<\/p>\n<h2>Customize Visual Studio Code keyboard shortcuts<\/h2>\n<p>Visual Studio Code also enables you to customize keyboard shortcuts. To customize keyboard shortcuts, select <strong>Command Palette<\/strong> from the <strong>View<\/strong> menu or press Ctrl+Shift+P (Cmd + P on the Mac), and then type <em>keyboard<\/em>. Then, select <strong>Preferences: Open Keyboard Shortcuts<\/strong>.<\/p>\n<p>Just like with user settings, Visual Studio Code will open two editor windows side-by-side. The editor window on the left, lists all the default keyboard shortcuts and is read-only. The editor window on the right is for your customized keyboard shortcuts.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5-HSG-120516.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5-HSG-120516.png\" alt=\"Editor window for customized keyboard shortcuts\" width=\"599\" height=\"458\" class=\"alignnone size-full wp-image-80955\" \/><\/a><\/p>\n<p>I generally don\u2019t override many of the default keyboard shortcuts. However, as a long-time Windows user, I expect Ctrl+Shift+S to execute <strong>Save All<\/strong> instead of <strong>Save As\u2026<\/strong>. You can change this keyboard shortcut like so:<\/p>\n<p style=\"padding-left: 30px\"><code>\/\/ Place your key bindings in this file to overwrite the defaults\n[<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>{ \"key\":\u00a0\u00a0\u00a0 \u00a0\"ctrl+shift+s\",<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>\"command\": \"workbench.action.files.saveAll\" }<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>]<\/code><\/p>\n<p>For more in-depth coverage of keyboard shortcuts, read the <a target=\"_blank\" href=\"http:\/\/code.visualstudio.com\/docs\/customization\/keybindings\">KeyBindings<\/a> article in the Visual Studio Code docs. Also, check out these handy keyboard shortcut cheat sheets for <a target=\"_blank\" href=\"https:\/\/code.visualstudio.com\/shortcuts\/keyboard-shortcuts-linux.pdf\">Linux<\/a>, <a target=\"_blank\" href=\"https:\/\/code.visualstudio.com\/shortcuts\/keyboard-shortcuts-macos.pdf\">macOS<\/a>, and <a href=\"https:\/\/code.visualstudio.com\/shortcuts\/keyboard-shortcuts-windows.pdf\">Windows<\/a>.<\/p>\n<p>In this blog post, we\u2019ve gotten our feet wet with Visual Studio Code. You should have it installed, understand the basic layout of editors and tool windows, and know how to customize settings and keyboard shortcuts. In the next blog post, we will investigate settings that you can configure per workspace and the PowerShell language features provided by the PowerShell extension for Visual Studio Code.<\/p>\n<p><strong>Keith Hill<\/strong>\nSoftware Engineer\nPowerShell MVP<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn about the features in Visual Studio Code that help you write PowerShell code. Since the PowerShell integrated scripting environment (ISE) shipped with Windows PowerShell 2.0 in 2009, the PowerShell landscape has changed significantly. In November 2014, Microsoft took .NET open source and cross-platform. Given PowerShell\u2019s dependence on .NET, this was an important prerequisite [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,641],"tags":[56,153,701,45],"class_list":["post-80915","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-windows-powershell","tag-guest-blogger","tag-keith-hill","tag-visual-studio-code","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn about the features in Visual Studio Code that help you write PowerShell code. Since the PowerShell integrated scripting environment (ISE) shipped with Windows PowerShell 2.0 in 2009, the PowerShell landscape has changed significantly. In November 2014, Microsoft took .NET open source and cross-platform. Given PowerShell\u2019s dependence on .NET, this was an important prerequisite [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=80915"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80915\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=80915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}