Home > Software design >  How do I create a new custom profile in Windows Terminal from a Powershell script?
How do I create a new custom profile in Windows Terminal from a Powershell script?

Time:10-05

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?

CodePudding user response:

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:

  • Git Bash
  • Ubuntu 22.04 for WSL

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' }
  • Related