Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active November 5, 2019 04:04
Show Gist options
  • Save jdhitsolutions/6e1f2a6e278d1915339813c94a03c557 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/6e1f2a6e278d1915339813c94a03c557 to your computer and use it in GitHub Desktop.
A proof of concept PowerShell function to add a new profile to the Windows Terminal Preview (0.2.1831.0).
Function New-WindowsTerminalProfile {
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullOrEmpty()]
[alias("name")]
[string]$ProfileName,
[Parameter( Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Commandline,
[string]$StartingDirectory,
[ValidateSet("Campbell", "Solarized Light", "SolarizedDark", "One Half Dark", "One Half Light")]
[string]$ColorScheme = "Campbell",
[string]$FontFace = "Consolas",
[int32]$FontSize = 10,
[ValidateSet("bar", "emptyBox", "filledBox", "underscore", "vintage")]
#cursor shape values are case sensitive
[string]$CursorShape = "bar",
[ValidatePattern("^#\w{6}$")]
[string]$CursorColor = "#FFFFFF",
[validateSet("PowerShell", "Linux", "CMD")]
[string]$Icon = "PowerShell",
[Validatescript( {Test-Path $_})]
[string]$BackgroundImage,
[ValidateSet("none", "fill", "uniform", "uniformtofill")]
[string]$StretchMode = "uniform",
[double]$Opacity = 0.9,
[switch]$EnableOpacity
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
#get the profiles.json file
$Path = (Get-Childitem $env:localappdata\packages\Microsoft.WindowsTerminal*\profiles.json -Recurse).fullname
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Getting settings from $Path"
#get the current settings
$settings = Get-content -Path $path | ConvertFrom-Json
#private helper function
Function _getdatestamp {
(Get-Date -format s).replace(":", "_")
}
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Creating terminal profile $ProfileName "
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Using icon for $icon"
switch ($Icon) {
"Powershell" { $iconpath = "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png"}
"Linux" { $iconpath = "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.scale-100.png"}
"CMD" { $iconpath = "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png"}
}
$guid = New-Guid
$new = @{
acrylicOpacity = $Opacity
closeOnExit = $True
colorScheme = $ColorScheme
commandline = $commandline
cursorColor = $CursorColor
cursorShape = $CursorShape
fontFace = $FontFace
fontSize = $FontSize
guid = "{$guid}"
historySize = 9001
icon = $iconPath
name = $ProfileName
padding = "0, 0, 0, 0"
snapOnInput = $True
useAcrylic = $EnableOpacity.ToBool()
}
if ($StartingDirectory) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Setting Starting Directory to $StartingDirectory"
#escape slashes in the path
$new.add("startingDirectory", $StartingDirectory)
}
if ($BackgroundImage) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Adding background image $BackgroundImage"
$new.Add("backgroundImage", $BackgroundImage)
$new.Add("backgroundImageStretchMode", $StretchMode)
$new.useAcrylic = $False
}
$newprof = New-Object -typename PSObject -property $new
$settings.profiles += $newprof
$out = [PSCustomobject]@{
globals = $settings.globals
profiles = $settings.profiles
schemes = $settings.schemes
}
if ($pscmdlet.ShouldProcess($($newProf | Out-String))) {
$parent = Split-Path -path $path
$backpath = Join-Path -Path $parent -ChildPath "profiles_$(_getdatestamp).json"
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Creating a backup copy of $Path to $Backpath"
Copy-item -path $path -Destination $backpath
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Writing updates to $Path"
$out | ConvertTo-Json -depth 3 | Out-File -FilePath $path
} #if should process
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
} #close New-WindowsTerminalProfile
@jdhitsolutions
Copy link
Author

@jdhitsolutions
Copy link
Author

These are some of the things I'm thinking about, depending on what happens with the application.

  • Going forward it would make sense to have many of these parameters take pipeline input by property name.
  • Need to do more testing on the case-sensitivity of some of the values.
  • I'd like a better way to get the current color schemes
  • Offer a validation set for font family
  • Need some parameter validation for parameters like font size and opacity

@jdhitsolutions
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment