Movatterモバイル変換


[0]ホーム

URL:


TryMCP servers to extend agent mode in VS Code!

Dismiss this update

PowerShell in Visual Studio Code

PowerShell is a task-based command-line shell and scripting language built on.NET thatprovides a powerful toolset for administrators on any platform.

The MicrosoftPowerShell extension for Visual Studio Code (VS Code) provides rich languagesupport and capabilities such as syntax completions, definition tracking, and linting forPowerShell. The extension should work everywhere VS Code is supported usingPowerShell 7+ currently supported versions.

Support for Windows PowerShell 5.1 is provided on a best effort basis..NET Framework 4.8 orhigher is required.

VS Code Remote Development Environments, includingGitHub Codespaces andVS Code Server are supported.

Important

Visual Studio Code for the Web is only supported for limited functionality such as basicsyntax highlighting, as the PowerShell engine can't run in this environment.

We actively test the following configurations:

  • Windows Server 2022 with Windows PowerShell 5.1 and PowerShell 7+
  • macOS 14.7 with PowerShell 7+
  • Ubuntu 24.04 with PowerShell 7+

On Windows, we also test with and without Constrained Language Mode enabled.

Installing the PowerShell extension

The PowerShell extension can be installed from the Visual Studio Code Marketplace by clicking theInstall Button. You can also install the PowerShell extension from within VS Code by openingtheExtensions view with keyboard shortcut⇧⌘X (Windows, LinuxCtrl+Shift+X), typingPowerShell,and selecting the PowerShell extension:

PowerShell extension

Major features

Debugging

The PowerShell extension uses the built-indebugging interface of VS Code to allow fordebugging of PowerShell scripts and modules. For more information about debugging PowerShell, seeUsing VS Code.

Multi-version support

You can configure the PowerShell extension to use any supported version of PowerShell installed onyour machine by followingthese instructions.

Or run thePowerShell: Show Session Menu command from the Command Palette(⇧⌘P (Windows, LinuxCtrl+Shift+P)).

CodeLens support

CodeLenses are a VS Code feature to provide actionable, contextual information that's displayedwithin the source code.

CodeLens features include:

  • PesterRun tests andDebug tests.

    Pester CodeLens Integration

  • Pester symbol support

    CodeLens Pester Symbol Support

  • Function, variable, class, and other symbol references

    CodeLens reference support shows the number of times a symbol is referenced within yourcode and allows you to jump to specific references.

    CodeLens Function Reference Support

PSScriptAnalyzer integration

PSScriptAnalyzer is a PowerShell module that provides a static source code checker for modulesand scripts.PSScriptAnalyzer has rules that verify the quality of PowerShell code. These rulesare based on PowerShell best practices identified by the PowerShell Team and the community.PSScriptAnalyzer generates diagnostic records (errors and warnings) to inform users aboutpotential code defects and suggests possible solutions for improvements.

The PowerShell extension includesPSScriptAnalyzer by default, and automatically performsanalysis on PowerShell script files you edit in VS Code.

PSScriptAnalyzer comes with a collection of built-in rules that check various aspects ofPowerShell source code such as presence of uninitialized variables, usage ofPSCredential type,usage ofInvoke-Expression, and others. The module also allows you to include or exclude specificrules.

To disablePSScriptAnalyzer, open your settings (⌘, (Windows, LinuxCtrl+,)), browseExtensions, select thePowerShell extension, and deselect the checkbox forScriptAnalysis: Enable (powershell.scriptAnalysis.enable).

PSScriptAnalyzer Settings

PSScriptAnalyzer also provides code formatting. You can invoke automatic document formattingwith theFormat Document command or the (⇧⌥F (WindowsShift+Alt+F, LinuxCtrl+Shift+I)) keyboard shortcut.

Pester integration

Pester is a framework for running unit tests to execute and Windows PowerShell 5.1 comes withPester 3.40 preinstalled. To updatePester or to install the latest version on otherplatforms, follow thePester installation instructions.

PowerShell extension settings

You can customize VS Codesettings from theFile >Preferences >Settings menu item.

You can also select the gear icon located in the lower left corner of the Activity Bar.

codeGear

You can also use the keyboard shortcut⌘, (Windows, LinuxCtrl+,) to open your settings.You can still open thesettings.json file usingPreferences: Open User Settings (JSON) command fromthe Command Palette (⇧⌘P (Windows, LinuxCtrl+Shift+P)) or by changing the default settings editorwith the"workbench.settings.editor" setting.

Go toUser and Workspace settings for more information on configuring VS Code settings.

Types.ps1xml and Format.ps1xml files

PowerShell.ps1xml files are used to extend the type system and define output formatting. For moreinformation on these files, see the official PowerShell documentation onTypes.ps1xml andFormat.ps1xml. You can get IntelliSense features when authoring.ps1xml files by installingtheXML extension by Red Hat. After installing, add this configuration to your user settings:

"xml.fileAssociations": [  {    "systemId":"https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/Schemas/Format.xsd",    "pattern":"**/*.Format.ps1xml"  },  {    "systemId":"https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/Schemas/Types.xsd",    "pattern":"**/*.Types.ps1xml"  }]

This configuration tells the XML extension to use the official XML schemas from the PowerShellrepository for all.ps1xml files. Configuring these schemas enables the following features inps1xml files:

  • Syntax error reporting
  • Schema validation
  • Tag and attribute completion
  • Autoclose tags
  • Symbol highlighting
  • Document folding
  • Document symbols and outline
  • Renaming support
  • Document formatting

Example scripts

Example scripts are included with the extension and can be found at the following path.

~/.vscode/extensions/ms-vscode.PowerShell-<version>/examples

To open or view the examples in VS Code, run the following from your PowerShell command prompt:

code (Get-ChildItem ~\.vscode\extensions\ms-vscode.PowerShell-*\examples)[-1]

You can also open the examples from the Command Palette (⇧⌘P (Windows, LinuxCtrl+Shift+P)) withthePowerShell: Open Examples Folder command.

Open PowerShell Examples

Additional resources

There are more detailed articles in the PowerShell documentation. Start withUsing VS Code.

Check out thetroubleshooting guide for answers to common questions.

For more information on debugging, check out theHey, Scripting Guy! two-part blog post serieswritten by@keithHill on debugging with the PowerShell extension:

Testing new features and providing feedback

We would encourage you to try thepre-release version whenever possible. When aPre-Release is available, it can be installed from the marketplace using theSwitch toPre-Release Version button. You can switch back to the stable version of the extensionby using theSwitch to Release Version button that will appear. You can also downgradeto other versions of the extension using the arrow next to theUninstall button andchoosingInstall Another Version....

Screenshot showing the button to switch to a pre-release version.

If you find a bug,open an issue and revert to the stable version while we fix it.

02/20/2025

[8]ページ先頭

©2009-2025 Movatter.jp