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 = "$home\desktop\$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 Files\PowerShell\7\pwsh.exe::PowerShell 7.1.1"
It pins "Administrator: PowerShell 7.1.1" to taskbar, however background color is black, I know the GUI way to make it RGB(1,36,86)
How can I use a command to add background color information to the shortcut itself?
Thank you for your help.
I have found something really useful, I used this command to install LinkParse3
:
pip install LnkParse3
It gives you a command: lnkparse
usage: lnkparse path\to\somelnkfile.lnk
I used it to parse some files and uploaded the result to Google Drive:
https://drive.google.com/file/d/1oJKXnr87pyiyM7So8gLlrTnpwUfsV3g2/view?usp=sharing
I have found all three PowerShell shortcuts has Color table set to 5645313, and all of them have RGB value of (1,36,86).
According to this answer: https://stackoverflow.com/a/48071483/14636788
When specifying an explicit RGB color, the COLORREF value has the following hexadecimal form:
0x00BBGGRR
So if I reverse the order of the RGB value:
PS C:\Windows\System32> [int]"0x562401"
5645313
Gotcha!
But the script doesn't modify .lnk files, and I am not really that good at Python, however I believe with a little tweak I can make it modify .lnk files...
The GitHub repository of LnkParse3
From what I have learned, .lnk
files use [MS-SHLLINK]: Shell Link (.LNK) Binary File Format
The property I need to change is Color Table
, it is located at:
EXTRA BLOCKS
→CONSOLE_PROPERTIES_BLOCK
→Color Table
In a program I need to use IShellLinkDataList
and set NT_CONSOLE_PROPS
property, however so far everything I can find doesn't allow me to modify .lnk files with PowerShell, I am still investigating...
It seems all I have to do is: IShellLinkDataList::AddDataBlock
however I don't know how to do it in PowerShell, but it shouldn't be hard...
I have finally found something really helpful:
https://github.com/neilpa/cmd-colors-solarized/blob/master/Get-Link.ps1
Though I have to admit it is really advanced and complex, I believe I can understand it and tweak it to my needs.
I have found that I need to use .NET/C# IShellLink library to achieve this, using methods mentioned above;
By Googling ShellLink.cs I was able to find bunch of relevant script files, however I only understand them partially, as I am not well versed in C#...
So, how can I write a PowerShell script that adds NT_CONSOLE_PROPS block to an existing .lnk file and set the Color Table attribute according to the RGB value supplied by using a ShellLink.cs file?