With PowerShell Pro Tools for Visual Studio Code 5.9 and later, you can now automate VS Code with PowerShell. As of the writing of this article, there are 18 cmdlets for interacting with the VS Code API. There are cmdlets for managing editors, terminals, editing documents, showing messages, accepting user input and setting text decorations.
This article provides a demonstration of each of the cmdlets’ features. You’ll need to install the PowerShell Pro Tools extension for VS Code. All these commands need to be run within the PowerShell Integrated Console or the PowerShell debugger.
Opening DocumentsÂ
Open documents by file name.
PS C:\> Open-VSCodeTextDocument -FileName .\form.designer.ps1
Closing Text Editors
Close editors that are already open.
PS C:\> Get-VSCodeTextEditor | Remove-VSCodeTextEditor
Getting Document Text
Get the text of a document. You can also pass in a range to select only a partial section of the text.
PS C:\> Get-VSCodeTextDocument | Get-VSCodeTextDocumentText
Inserting Text
Inserts text into a particular position in the selected document. This creates an edit but does not save the file.
PS C:\> $position = New-VSCodePosition -Line 0 -Character 2 PS C:\> Get-VSCodeTextDocument | Add-VSCodeTextDocumentText -Position $position -Text NewText
Removing Text
Removes a range of text from a document. This creates an edit but does not save the file.
PS C:\> $Range = New-VSCodeRange -StartLine 0 -EndLine 0 -StartCharacter 0 -EndCharacter 10 PS C:\> Get-VSCodeTextDocument | Remove-VSCodeTextDocumentText -Range $Range
Setting Text Decorations
Decorates a range of text with an optional set of colors, outlines, borders, and text.
PS C:\> $Range = New-VSCodeRange -StartLine 0 -EndLine 0 -StartCharacter 0 -EndCharacter 55 PS C:\> Get-VSCodeTextEditor | Set-VSCodeTextEditorDecoration -BackgroundColor 'descriptionForeground' -Range $Range -Key 12321 -FontWeight bold
You can clear decorations by using the Clear-VSCodeTextEditorDecoration cmdlet. If you want to only clear a single decoration, you can specify the key.
Sending Text to a Terminal
Sends text to the specified terminal. You can commit this text by including the -AddNewLine parameter.
PS C:\> Get-VSCodeTerminal | Where-Object Name -eq 'PowerShell Integrated Console' | Send-VSCodeTerminalText -Text 'Write-Host "Hello World!"'
Showing Messages
Show a message to the user. You can show information, warning, and error messages.
PS C:\> Show-VSCodeMessage -Message 'Error!!!' -Type Error
Showing a Message with a Response
Show a message to the user and provide an option for them to select.
PS C:\> Show-VSCodeMessage -Message 'What should we do?' -Items @('Party', 'Sleep')
Showing a Quick Pick List
Shows a quick pick list for a user to select items from. This cmdlet will return the user’s selection to PowerShell.
PS C:\> Show-VSCodeQuickPick -PlaceHolder 'What should we do?' -Items @('Party', 'Sleep')
Showing an Input Box
Shows an input box for the user to enter arbitrary text. This cmdlet will return the result to PowerShell.
PS C:\> Show-VSCodeInputBox -PlaceHolder 'Enter some text'
Set Status Bar Message
Sets the status bar message.
PS C:\> Set-VSCodeStatusBarMessage -Message 'Hellllloooo'
Looking for more features?Â
The VS Code API is vast and there are more features we could add. Feel free to submit an issue on our GitHub page.