Setting Up Powershell In Visual Studio For Mac

Posted on  by 

  1. X Code
  2. Visual Studio Community For Mac

Summary: Learn about the features in Visual Studio Code that help you write PowerShell code.

Here are the steps to ensure proper settings of VSTS Mac build agents using this plan: Important Note: You are provided ONE complementary 'private' agent slot in your Microsoft Visual Studio Team Services (VSTS) account. You may use this agent slot with our MacinCloud VSTS Build Agent Plan. For acquiring more VSTS agent slots, please visit HERE. 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).

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

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

X Code

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
[

Visual

{ '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

Active1 year, 6 months ago

I can't update update-help on powershell mac osthe error is :

Update-Help : Failed to update Help for the module(s) 'Microsoft.PowerShell.Archive, Microsoft.PowerShell.Core, Microsoft.PowerShell.Host, Microsoft.PowerShell.Management, Microsoft.PowerShell.Security, Microsoft.PowerShell.Utility, PackageManagement, PowerShellGet, PSReadLine' with UI culture(s) {en-US} : Access to the path '/usr/local/microsoft/powershell/6.0.1/en-US/Microsoft.PowerShell.Commands.Management.dll-Help.xml' is denied.At line:1 char:1+ Update-Help + ~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Update-Help], Exception + FullyQualifiedErrorId : UnknownErrorId,Microsoft.PowerShell.Commands.UpdateHelpCommand

EBGreen
27.5k11 gold badges54 silver badges78 bronze badges
aaaaaa

1 Answer

Microsoft visual studio for mac

Like you I don't spend much time on OSX, but I just did Update-Help on a default OSX install, latest version without use su and it updated just fine.

Try using the -force parameter.

Are you using a different language on your MAC. If you are try setting the culture to US English.

I have seen Update-Help fail even on Windows, if you are not running the VSCode / PowerShell session as admin, or if you are using a non-English OS SKU. So, then you'd do this...

Back in 2016, Update-Help failed for non-Windows clients. See this post.

I'm new to PowerShell and I know on PowerShell on windows you need to run as administrator to update the help files, but how do you update help files on PowerShell on Mac?

But again, I read they got that fixed a while back and as noted, I just did this, and it works fine, even without / with su.

As for some of the errors you are seeing, that is not uncommon to see certain modules not be able to update from time to time. I've seen this more than I care to over the years. I normally just suppress those errors these days.

This does not mean nothing updated as you'd see form the verbose output.

Powershell command 'update-help' fails

When attempting to update the help information on a Windows 10 Pro machine that is joined to a domain, I get this error message:

Update-Help fails to update 2 modules on Windows 10 Pro x64 #1766 https://github.com/PowerShell/PowerShell/issues/1766

postanotepostanote
5,1082 gold badges4 silver badges11 bronze badges

Visual Studio Community For Mac

Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not the answer you're looking for? Browse other questions tagged powershellvisual-studio-code or ask your own question.

Coments are closed