powershell – How to change the background color of the console window a .lnk opens programmatically?

The title says it all. How to change the background color of the console window a .lnk opens programmatically?

For example, using my script from my previous answer: pintotaskbar.ps1:

This creates a shortcut on desktop:

$shortcutpath         = "$homedesktop$name.lnk"
$wshshell             = new-object -comobject wscript.shell
$shortcut             = $wshshell.createshortcut($shortcutpath)
$shortcut.targetpath  = $path
$shortcut.save()

This makes the shortcut start the program elevated:

$bytes                = (system.io.file)::readallbytes($shortcutpath)
$bytes(0x15)          = $bytes(0x15) -bor 0x20
(system.io.file)::writeallbytes($shortcutpath,$bytes)

I used this code to pin PowerShell 7.1.1 to taskbar:

pintotaskbar.ps1 "C:Program FilesPowerShell7pwsh.exe::PowerShell 7.1.1"

It pins “Administrator: PowerShell 7.1.1” to taskbar, however background color of the window it opens is black, I would like it to have PowerShell “noble blue” RGB(1,36,86) as background color, I know the GUI way to do this:

enter image description here

How can I achieve the same result with a command?

Since all information about the shortcut is stored in the .lnk file, and I can make the shortcut elevated by changing a byte, it should also be possible to change the background color by changing three bytes in the .lnk file…

The problem is: I don’t know which bytes to change.

Thank you for your help.


I think I have found something:

I ran these commands in PowerShell 7.1.1:

$bytes1= (system.io.file)::readallbytes("C:UsersEstrangerDesktopPowerShell 7.1.1.lnk")
$bytes2= (system.io.file)::readallbytes("C:UsersEstrangerAppDataRoamingMicrosoftInternet ExplorerQuick LaunchUser PinnedTaskBarPowerShell 7.1.1.lnk")
$bytes1.count
$bytes2.count
for ($i=0;$i -lt 1518;$i++) {if ($bytes2($i) -eq 1 -and $bytes2($i+1) -eq 36 -and $bytes2($i+2) -eq 86) {$i}}
$index=for ($i=0;$i -lt 1518;$i++) {if ($bytes2($i) -eq 1 -and $bytes2($i+1) -eq 36 -and $bytes2($i+2) -eq 86) {$i}}
$bytes2($index)
$bytes2($index+1)
$bytes2($index+2)
$bytes1($index)
$bytes1($index+1)
$bytes1($index+2)

And the results:

enter image description here

PS C:WindowsSystem32> $bytes1= (system.io.file)::readallbytes("C:UsersEstrangerDesktopPowerShell 7.1.1.lnk")
PS C:WindowsSystem32> $bytes2= (system.io.file)::readallbytes("C:UsersEstrangerAppDataRoamingMicrosoftInternet ExplorerQuick LaunchUser PinnedTaskBarPowerShell 7.1.1.lnk")
PS C:WindowsSystem32> $bytes1.count
1046
PS C:WindowsSystem32> $bytes2.count
1518
PS C:WindowsSystem32> for ($i=0;$i -lt 1518;$i++) {if ($bytes2($i) -eq 1 -and $bytes2($i+1) -eq 36 -and $bytes2($i+2) -eq 86) {$i}}
1008
PS C:WindowsSystem32> $index=for ($i=0;$i -lt 1518;$i++) {if ($bytes2($i) -eq 1 -and $bytes2($i+1) -eq 36 -and $bytes2($i+2) -eq 86) {$i}}
PS C:WindowsSystem32> $bytes2($index)
1
PS C:WindowsSystem32> $bytes2($index+1)
36
PS C:WindowsSystem32> $bytes2($index+2)
86
PS C:WindowsSystem32> $bytes1($index)
0
PS C:WindowsSystem32> $bytes1($index+1)
104
PS C:WindowsSystem32> $bytes1($index+2)
0
PS C:WindowsSystem32>

It seems I was right, but the pinned shortcut has more bytes than the shortcuts on desktop, so I don’t know the correct index of the color bytes…