Setting Up Powershell In Visual Studio For Mac

Open or create a Visual Studio for Mac Project. Right click on the Project and select Version Control > Publish in Version Control. In the Connect to Repository tab, select Subversion from the top drop-down. As of Visual Studio Code version 1.9 you can debug PowerShell scripts without having to open the folder containing the PowerShell script. 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. Open or create a Visual Studio for Mac Project. Right click on the Project and select Version Control > Publish in Version Control. In the Connect to Repository tab, select Subversion from the top drop-down. Over the years, PowerShell developers have used tools like Quest PowerGUI and Visual Studio with the PowerShell Tools Extension to write, test, and debug their PowerShell code. In fact, not all that long ago, I wrote about moving from PowerGUI over to Visual Studio for your PowerShell development work.
Summary: Learn about the features in Visual Studio Code that help you write PowerShell code.
In Part 1 of this 2 part tutorial series on integrating Xamarin into a continuous build environment, I detailed how to use Jenkins to build Xamarin.iOS and Xamarin.Android projects and a Team Foundation Build server to build Windows Phone Projects. In Part 2, I’ll detail how you can take advantage of Microsoft’s new Build.vNext technology that is a part of Visual Studio Online (VSO). Now with F5 and debugging, it is up to you whether you want to change those around so they match up to what PowerShell ISE did. You would just need to find the command from the keyboard settings and add that to your keyboard settings. Aug 26, 2016 Showing how to Download, Install and Run PowerShell on your Mac. I also show briefly how to set PowerShell as the default terminal in Visual Studio 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’s 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 open source on GitHub? Detecting a trend here?
To support cross-platform PowerShell script development, Microsoft started two new open-source projects. The PowerShell Editor Services 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 OmniSharp, which provides C# language support to editors like Atom, Sublime, Vim, and Visual Studio Code. The other project is vscode-powershell, which is a Visual Studio Code extension that integrates the PowerShell Editor Services. Both projects have taken significant community contributions.
Choose a PowerShell scripting environment
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. Here are a few reasons why you might want to consider using Visual Studio Code:
- Enhanced editor services
- Open folder for editing (easily access multiple files)
- Peek/Goto definition
- Find all references
- Search and replace across multiple files
- Go to Symbol
- Task runner (configure to run Pester tests)
- Toggle render whitespace
- Multiple cursor support (edit multiple lines at once)
- Background script analysis by using PSScriptAnalyzer with code fix support coming soon
- Enhanced debugging
- Variables window with support for setting variables during debug
- Watch window for watching variable values
- Breakpoints window with support for line, function, and conditional breakpoints
- Breakpoint hit count support
- Call Stack window
- Built-in support for Git version control
- Support for multiple color schemes including dark and light schemes
- Support for other file types: Batch, CSS, Dockerfile, Html, Ini, JavaScript, JSON, Markdown, Perl, Python, SQL, YAML, etc.
- Rich extension ecosystem
- mssql, Docker Support, Git History and over a thousand other extensions
Visual Studio For Mac Os
On the other hand, if PowerShell ISE meets your needs, then by all means continue to use it. It’s 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’t do – yet. Here are the major features unique to PowerShell ISE:
- Remote runspace tabs
- Remote debugging
- Language aware code folding
- Interactive PowerShell console available during edit and debug
Install and configure Visual Studio Code
To install Visual Studio Code for your operating system, open http://code.visualstudio.com in a browser and follow the instructions. Even without the PowerShell extension installed, you will get syntax colorization and basic autocompletion for PowerShell scripts.
Syntax colorization is specified via a TextMate tmLanguage file. PowerShell syntax colorization is currently provided by the PowerShell Editor Syntax project on GitHub. If you find an issue with PowerShell syntax colorization, be sure to submit the issue to that project.
To get the full PowerShell script development experience, let’s install the PowerShell extension for Visual Studio Code. First, select Extensions from the View menu (or press Ctrl+Shift+X on Windows). In the Extensions view, type PowerShell in the search box, and then press Enter. The PowerShell extension from Microsoft should show up at the top of the search results.
Click the Install 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 Reload button that appears where the Install button used to be.
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 releases for all platforms on GitHub and instructions to install PowerShell on these platforms.
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 “Untitled-1”). 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 Command Palette from the View menu, or press Ctrl+Shift+P (Cmd+P on Mac). Type change language mode or use fuzzy matching by typing clm, andthen press Enter. After you’re in Select Language Mode, type ps to select PowerShell, and then press Enter.
Visual Studio Code basics
The basic layout of Visual Studio Code is shown in the following screenshot:
The View Bar (A) lets you to quickly switch between the Explorer, Search, Git, Debug, and Extensions views. The Side Bar (B) is collapsible (Ctrl+B / Cmd+B on macOS), so you can maximize the space that’s 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’s position, the current file’s encoding and line-endings, as well as the selected language mode for the current file. You 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 The Basics of Visual Studio Code.
Customize Visual Studio Code settings
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 Command Palette from the View menu, or press Ctrl+Shift+P (Cmd + P on the Mac), type user, and then select Preferences: Open User Settings. This will open two editor windows as shown in the following screenshot:
As you can see in the Default Settings 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 “default” 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 “Light+” color scheme.
Following is a basic set of customizations that I would recommend for PowerShell development:
// Place your settings in this file to overwrite the default settings
{
'editor.rulers': [ 120 ],
'files.trimTrailingWhitespace': true,
'terminal.integrated.shell.windows':
'C:WINDOWSsysnativeWindowsPowerShellv1.0powershell.exe'
}
The editor ruler provides a visual reminder of where you might want to wrap your PowerShell script. The trimTrailingWhitespace setting is particularly convenient for PowerShell. If you use ` for line continuation, you’ll never have to worry about inadvertent whitespace after a `. Finally, you will likely want Visual Studio Code’s Integrated Terminal to open PowerShell instead of CMD.exe.
The PowerShell extension comes with the following customizable settings:
You can get more in-depth coverage of user settings in the User and Workspace Settings article in the Visual Studio Code docs.
Customize Visual Studio Code keyboard shortcuts
Visual Studio Code also enables you to customize keyboard shortcuts. To customize keyboard shortcuts, select Command Palette from the View menu or press Ctrl+Shift+P (Cmd + P on the Mac), and then type keyboard. Then, select Preferences: Open Keyboard Shortcuts.
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.
I generally don’t override many of the default keyboard shortcuts. However, as a long-time Windows user, I expect Ctrl+Shift+S to execute Save All instead of Save As…. You can change this keyboard shortcut like so:

// Place your key bindings in this file to overwrite the defaults
[
{ 'key': 'ctrl+shift+s',
'command': 'workbench.action.files.saveAll' }
]
For more in-depth coverage of keyboard shortcuts, read the KeyBindings article in the Visual Studio Code docs. Also, check out these handy keyboard shortcut cheat sheets for Linux, macOS, and Windows.
In this blog post, we’ve 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.
Keith Hill
Software Engineer
PowerShell MVP
In a blog post on November 16th 2015 Microsoft announced PowerShell support in Visual Studio Code. And as it befits the “new” Microsoft, this PowerShell extension for Visual Studio Code (VSC) was placed on GitHub.
VSC offers features that are currently not present in the PowerShell ISE, one of the more important ones for now being Git support. And another, not negligible feature, VSC is a free product. On the VMware{code} powercli Slack channel, which you should check out, the new editor was discussed briefly.
With the 0.6.0 release of PowerShell for VS@Code, a couple of important new features were introduced. For the PowerCLI users, the addition of a VSC specific ‘profile‘, makes editing your PowerCLI scripts in VSC a lot easier. Want to try it out, read on!
Setting up VSC for PowerCLI
PowerCLI, at the time of writing, still consists of a number of PSSnapins and Modules. In my Universal PowerCLI Loader post, I introduced a generic function, named Enable-PowerCLI, that will, for each official PowerCLI release, make sure that all PSSnapins and Modules (from v6.* onwards) are loaded. We will use this function to set up PowerCLI in our VSC.
The VSC profile is stored in a file called “%UserProfile%DocumentsWindowsPowerShellMicrosoft.VSCode_profile.ps1“.
In that file I store my Enable-PowerCLI function, and at the end of the file I call the function.
Since the PowerShell for VS@Code 0.6.1 release, the execution of the profile.ps1 is set to true by default. You can skip the following paragraph.
In the 0.6.0 release, the execution of the profile was not set by default. You could enable this via the <File><Preferences><User Settings> menu entry. In the editor the default settings and your personal settings.json file are opened. In your settings.json file, enter the line “powershell.enableProfileLoading”: true, this will make sure the profile.ps1 is executed the next time you open VSC.
Now restart your VSC, and lo and behold, all PowerCLI cmdlets are known and available under Intellisense.
Snippets
In VSC there is a snippet feature, which allows you to define a code block that can be called by using a shortname in the editor. This is a handy way to have your PowerCLI code templates available at all times, no need to learn by hearth or “feeling lucky” in the search engine of your choice.
A snippet is packaged in a JSON file. The snippets file for PowerShell is “%appdata%CodeUsersnippetspowershell.json”. In that file you have one or more snippets of code.
The following is a short example of template code for the Get-Stat cmdlet for VMs.
In the snippets folder in the powershell.json file we add the definition for the PCLIStatVM snippet.

We can now call the snippet by just entering the name.
When we hit <Enter>, while the entry is selected, the code will be inserted in our current document.
The syntax of a VSC snippet is documented in the online VSC documentation.
PowerShell Editor Services
The PowerShell Editor Services offer a common extensibility model which allows one to write extension code in PowerShell. In the 0.6.0 release this is now made available through the $psEditor object and the Register-EditorCommand cmdlet. More information can be found in the, also newly introduced, PowerShell Editor Services documentation.
As an example of the use of the Register-EditorCommand I created the following example. It adds a command to the VSC that allows me to open a browser with the pages I normally use as reference and help when I work with PowerCLI. Add the following code to your profile.ps1 file.
2 4 6 8 10 12 14 | $browser='chrome.exe' $pclisites='https://communities.vmware.com/community/vmtn/automationtools/powercli/content?filterID=contentstatus[published]~objecttype~objecttype[thread]', 'https://www.vmware.com/support/developer/PowerCLI/PowerCLI63R1/html/index.html', 'http://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/right-pane.html', 'http://lucd.info' Start-Process$browser$pclisites -Name'PowerCLI.HelpSites'` -ScriptBlock$pclihelp |
In the VSC you can now use this new command as the following short video will show.
X Code
A second example will actually allow you to act on selected text in the editor. With the EditorContext object you can actually what the user has selected, and take action accordingly. In this trivial example it takes the PowerCLI cmdlet that was selected, and open the online Help page for that cmdlet. Note that there is no checking implemented whatsoever, if you select something else than a PowerCLI cmdlet, the browser will go to an invalid page.
Visual Studio For Mac Download
2 4 6 8 10 12 14 | param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context) $cmdlet=$context.CurrentFile.GetText($context.SelectedRange) $cmdhelp='https://www.vmware.com/support/developer/PowerCLI/PowerCLI63R1/html/$($cmdlet).html' Start-Process$browser$cmdhelp -Name'PowerCLI.HelpCmdlet'` -ScriptBlock$pclicmdhelp |
And a short video that shows how this works.
- Select the PowerCLI cmdlet
- Press <F1>
- Type ‘ps ad’ and the fuzzy search will bring you to the additional commands menu
- Select the PowerCLI Cmdlet Help
In this third example, I show how you can interrogate the AST and also how you can send feedback to the user. This command counts the different cmdlets and variables that are present in the file. And displays the result in the Output area.
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 | param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context) $cmdArr=@() foreach($tokenin$context.CurrentFile.Tokens){ 'StringLiteralToken'{ $cmdArr+=$token.Value } $varArr+=$token.Name } $varArr=$varArr|Sort-Object-Unique Write-Output'You used $($cmdArr.Count) different cmdlets' Write-Output'You used $($varArr.Count) different variables' } Register-EditorCommand` -DisplayName'Count Cmdlets/Variables'` |
And a short video to demonstrate.
At the end of this post, you can download a sample VSC profile.ps1, it contains the PowerCLI Universal Loader function and the Register-EditorCommand example.
I’m currently still exploring all the possibilities of the the VS@Code and PowerShell for VS@Code. There is a whole lot more to discover, and I intend to keep this post updated with new discoveries and additional features that will come in future releases.
If you want to play with VSC and the PowerShell feature, there are a couple of excellent blog posts from Keith Hill, that will help you get started.
Getting Started with Visual Studio Code for Use with PowerShell
Debugging PowerShell Script with Visual Studio Code
And there is of course the extensive, online VSC documentation.
Enjoy!
Microsoft.VSCode_profile.ps1
Details...