Automating Visual Studio Code
Automate Visual Studio Code with PowerShell

Overview

Using PowerShell Pro Tools for Visual Studio Code you can automate the editor itself. This allows you to script repetitive actions you may take within Visual Studio Code. You can edit documents, show information and more.

Getting Started

To automate Visual Studio Code, you will need to first import the PowerShell Pro Tools VS Code module .
1
PS C:\> Import-Module PowerSHellProTools.VSCode
Copied!
Once the module is loaded, you can begin running commands.
1
PS C:\> Get-VSCodeTerminal
2
3
4
Name : pwsh
5
Id : 1
6
Columns : 0
7
Rows : 0
8
CreationOptions : - C:\Program Files\PowerShell\7\pwsh.exe
9
10
Name : PowerShell Integrated Console
11
Id : 2
12
Columns : 140
13
Rows : 13
14
CreationOptions : PowerShell Integrated Console - C:\Program Files\PowerShell\7\pwsh.exe
Copied!

Available Commands

Opening Documents
Open documents by file name.
1
PS C:\> Open-VSCodeTextDocument -FileName .\form.designer.ps1
Copied!
Closing Text Editors
Close editors that are already open.
1
PS C:\> Get-VSCodeTextEditor | Remove-VSCodeTextEditor
Copied!
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.
1
PS C:\> Get-VSCodeTextDocument | Get-VSCodeTextDocumentText
Copied!
Inserting Text
Inserts text into a particular position in the selected document. This creates an edit but does not save the file.
1
PS C:\> $position = New-VSCodePosition -Line 0 -Character 2
2
PS C:\> Get-VSCodeTextDocument | Add-VSCodeTextDocumentText -Position $position -Text NewText
Copied!
Removing Text
Removes a range of text from a document. This creates an edit but does not save the file.
1
PS C:\> $Range = New-VSCodeRange -StartLine 0 -EndLine 0 -StartCharacter 0 -EndCharacter 10
2
PS C:\> Get-VSCodeTextDocument | Remove-VSCodeTextDocumentText -Range $Range
Copied!
Setting Text Decorations
Decorates a range of text with an optional set of colors, outlines, borders, and text.
1
PS C:\> $Range = New-VSCodeRange -StartLine 0 -EndLine 0 -StartCharacter 0 -EndCharacter 55
2
PS C:\> Get-VSCodeTextEditor | Set-VSCodeTextEditorDecoration -BackgroundColor 'descriptionForeground' -Range $Range -Key 12321 -FontWeight bold
Copied!
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.
1
PS C:\> Get-VSCodeTerminal | Where-Object Name -eq 'PowerShell Integrated Console' | Send-VSCodeTerminalText -Text 'Write-Host "Hello World!"'
Copied!
Showing Messages
Show a message to the user. You can show information, warning, and error messages.
1
PS C:\> Show-VSCodeMessage -Message 'Error!!!' -Type Error
Copied!
Showing a Message with a Response
Show a message to the user and provide an option for them to select.
1
PS C:\> Show-VSCodeMessage -Message 'What should we do?' -Items @('Party', 'Sleep')
Copied!
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.
1
PS C:\> Show-VSCodeQuickPick -PlaceHolder 'What should we do?' -Items @('Party', 'Sleep')
Copied!
Showing an Input Box
Shows an input box for the user to enter arbitrary text. This cmdlet will return the result to PowerShell.
1
PS C:\> Show-VSCodeInputBox -PlaceHolder 'Enter some text'
Copied!
Set Status Bar Message
Sets the status bar message.
1
PS C:\> Set-VSCodeStatusBarMessage -Message 'Hellllloooo'
Copied!
Last modified 1yr ago