Reputation: 928
I'm creating a PowerShell script to help me install and configure software on new PCs. After issuing a PowerShell command to install Windows Terminal, I would like to add additional commands that set up custom profiles. It's easy enough to use the Terminal GUI to create these profiles (Settings->Add a new profile->etc.), but I'd like to automate it.
Is my only option finding and editing Terminal's settings.json directly? Or are there API calls that make this possible?
Upvotes: 2
Views: 1869
Reputation: 1825
A COMPACT version
$file = "$env:localappdata\Microsoft\Windows Terminal\settings.json"; copy $file $file".old"
$json = Get-Content -Path $file -Raw | ConvertFrom-Json
$newItem = [PSCustomObject]@{
name = "Powershell 7"
commandline = """C:\\Program Files\\PowerShell\\7\\pwsh.exe"" -WorkingDirectory c:\\temp"
font = [PSCustomObject]@{size = 10}
}
$json.profiles.list += $newItem
Write-Output $json | ConvertTo-Json -Depth 10 | Out-File $file -Encoding Utf8
A verbose version
Backup current json file:
$file = "$env:localappdata\Microsoft\Windows Terminal\settings.json"
copy $file $file".old"
Read and convert json-file to a PS object:
$json = Get-Content -Path $file -Raw | ConvertFrom-Json
Assign the array-object to $profiles
$profiles = $json.profiles.list
Create a new item in the array-object (in this case a new profile for powershell7):
$newItem = [PSCustomObject]@{
name = "Powershell 7_"
commandline = """C:\\Program Files\\PowerShell\\7\\pwsh.exe"" -WorkingDirectory c:\\temp"
font = [PSCustomObject]@{size = 10}
}
Add the new array-item to the array:
$json.profiles.list += $newItem
Write new json-document:
Write-Output $json | ConvertTo-Json -Depth 10 | Out-File $file -Encoding Utf8
If you want to see the content of a json object for debugging:
$json | ConvertTo-Json -Depth 10
Upvotes: 0
Reputation: 20899
Windows Terminal has a feature known as JSON Fragment Extensions for developers to add additional profiles without needing to directly modify the settings.json
.
The hardest part is creating the correct GUID for your profile name -- I'm not 100% sure on the process myself, having not tried it personally, but at least there is sample Python code on that page for that. Once you determine the profile's GUID, you can hardcode it -- No need to do it programmatically during installation.
Since it doesn't sound like you are developing a Store app, you'd likely be considered an "app installed from the web" for the purposes of where to place the fragment. You can either put it in C:\ProgramData\Microsoft\Windows Terminal\Fragments\{app-name}\{file-name}.json
for all users on the system or C:\Users\<user>\AppData\Local\Microsoft\Windows Terminal\Fragments\{app-name}\{file-name}.json
for individual users.
Note that since you say you will be using PowerShell for the installation, the doc mentions that you must use UTF-8 encoding (E.g. Out-File $fragmentPath -Encoding Utf8
).
If you'd like to see some examples of real, working JSON fragments, the following applications that I'm aware of utilize the feature:
You can find the Ubuntu one (and possible some others) by starting an Administrative PowerShell and running:
Get-ChildItem -Recurse 'C:\Program Files\WindowsApps\' | Where-Object {$_.Name -like 'terminal.json' }
Upvotes: 2