Recently I learned about some features in Windows terminal that I always wanted but never spent time looking into until it was mentioned in this blog post.
The following features are available in versions 1.21 of Windows Terminal. One way to upgrade is to use winget:
winget upgrade terminal
One of the features is the Scratchpad which is an experimental feature introduced in the Windows Terminal Preview 1.21. This is useful to edit some code before pasting in the terminal to execute. The other feature is snippets which you can insert into the terminal window. This is useful for long commands that are used frequently.
Before using these features you need to add a specific action to the settings.json of the Windows Terminal. It helps to have some understanding of JSON data types before diving in.
JSON Primer
Objects are enclosed in curly braces and they con contain one or more key/value pairs, in the following example SplitMode is the key and Duplicate is the value. JSON keys (aka names) require double quotes.
{
"splitMode": "duplicate"
}
Values in JSON can also be objects (the key is “Command”), If there are more than one key/value pairs they are separated by commas.
"command":
{
"action": "splitPane",
"split": "auto",
"splitMode": "duplicate"
}
Arrays are lists, they can consist of one or more JSON data types. JSON objects (ie, curly braces) can contain arrays, in the following example the the value of the IpAddresses key is an array:
{
"IpAddresses":["192.168.100.1", "192.168.200.1"]
}
Open your terminal settings
To open your settings.json file press (Ctrl + Shift + ,). It should open in notepad or VSCode. Then look for a key called “actions”, the value is an array (square brackets) and each item in the array is an object (curly brackets). If you look for the corresponding square bracket containing those objects you will need to insert another object with the actions. See the JSON below and notice where it says Insert Here. I have removed some of the items to make the block shorter
"actions":
[
{
"command":
{
"action": "copy",
"singleLine": false
},
"id": "User.copy.644BA8F2",
"keys": "ctrl+c"
},
....
{
"command":
{
"action": "splitPane",
"split": "auto",
"splitMode": "duplicate"
},
"id": "User.splitPane.A6751878",
"keys": "alt+shift+d"
}
< Insert here >
]
ScratchPad
Becasuse the following object is another item in a array you need to insert a comma just before it, therefore you would paste this into the actions array. The keys can be any of your choice but make sure it doesn’t conflict with existing keyboard shortcuts. I chose Ctrl + Alt + O.
,{
"command": {
"action": "splitPane",
"type": "scratchpad"
},
"keys": "ctrl+alt+o"
}
Pressing Ctrl + Alt + O will open a scratch pad to the right of the console
Close by pressing Ctrl + Shift + W when it's in focus
Snippets
Just like in the example above you will need to add a new object for each snippet you need. You would just edit the place holders for the code and the name.
,{
"command": {
"action": "sendInput",
"input": "<Insert code here>"
},
"name": "<Insert name here>"
}
Below are some ideas of some code which I use regularly. Note that if you want several commands in a snippet you will need to separate each command with semi-colons (;).
Insert Snippets
To search for snippets you need to press Ctrl+Shift+P to open the command pallete. Then just search for your snippet.
Snippet ideas
Unlock Secret store
I use this one to unlock my secret vault,
$passwordPath = Join-Path (Split-Path $profile) SecretStore.vault.credential;$password = Import-CliXml -Path $passwordPath;Unlock-SecretStore -Password $password"
For a good tutorial on how to use the PowerShell secrets module see How to manage PowerShell secrets with the SecretsManagement module
Get Active Directory computers
If you regularly search for AD computers in acertain OU you could add that as a snippet and name it “Get-AdComputer for Sales”.
Get-ADComputer -SearchBase 'OU=sales,DC=contoso,DC=com' -Filter
The following are courtesy of Guy Leech.
Group policy logs
$boottime = (gcim win32_operatingSystem).LastBootUpTime;Get-WinEvent -Oldest -FilterHasht @{ ProviderName = 'Microsoft-Windows-GroupPolicy' ; StartTime = $boottime ; EndTime = $boottime.AddSeconds( 180 ) }
Account lockouts
Get-WinEvent -FilterHashtable @{ LogName = 'Security' ; Id = 4740 ; StartTime = [datetime]::Now.AddMinutes( -30 ) } -ComputerName dc1 |select timecreated,@{n='account';e={$_.properties[0].value}},@{n='from';e={$_.properties[1].value}}
Conclusion
The addition of code snippets allows for quick access to frequently used commands, streamlining workflows. Moreover, the scratch pad feature provides a space for drafting and refining complex commands, making the terminal more versatile and user-friendly.
Resources
New in Windows Terminal: Restore buffers, code snippets, scratchpad and regex – 4sysops
https://x.com/guyrleech/status/1751890992062406722
https://x.com/guyrleech/status/1751890992062406722