-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathExtensionExamples.ps1
53 lines (39 loc) · 1.71 KB
/
ExtensionExamples.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Instructions: select the entire file and hit F8 to
# load the extensions. To see the list of registered
# extensions and run them, hit Ctrl+Shift+P, type 'addi'
# and run the "Show additional commands from PowerShell modules"
# command. A quick pick list will appear with all 3
# extensions registered. Selecting one of them will launch it.
function Invoke-MyCommand {
Write-Output "My command's function was executed!"
}
# Registering a command for an existing function
Register-EditorCommand -Verbose `
-Name "MyModule.MyCommandWithFunction" `
-DisplayName "My command with function" `
-Function Invoke-MyCommand
# Registering a command to run a ScriptBlock
Register-EditorCommand -Verbose `
-Name "MyModule.MyCommandWithScriptBlock" `
-DisplayName "My command with script block" `
-ScriptBlock { Write-Output "My command's script block was executed!" }
# A real command example:
function Invoke-MyEdit([Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context) {
# Insert text at pre-defined position
$context.CurrentFile.InsertText(
"`r`n# I was inserted by PowerShell code!`r`nGet-Process -Name chrome`r`n",
35, 1);
# TRY THIS ALSO, comment out the above 4 lines and uncomment the below
# # Insert text at cursor position
# $context.CurrentFile.InsertText(
# "Get-Process -Name chrome",
# $context.CursorPosition);
}
# After registering this command, you only need to re-evaluate the
# Invoke-MyEdit command when you've made changes to its code. The
# registration of the command persists.
Register-EditorCommand -Verbose `
-Name "MyModule.MyEditCommand" `
-DisplayName "Apply my edit!" `
-Function Invoke-MyEdit `
-SuppressOutput