3

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)

enter image description here

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 BLOCKSCONSOLE_PROPERTIES_BLOCKColor 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?

0

3 Answers 3

3
+50

The NT_CONSOLE_PROPS structure contains the 16 colors in the COLORREF format that are displayed in the Properties/Colors tab, which is called ColorTable in the article.

The color that is chosen by default is the first one, which is the color black.

You may change the colors in the link by using the Get-Link.ps1 PowerShell script that you have already found, without needing to understand it.

To change the black color to another color, the code might look like this, based on an archived solarizing script:

[CmdletBinding()]
    param($Path = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell.lnk" )
    # Requires the "Get-Link script"
    $lnk = Get-Link $Path
    $lnk.ConsoleColors[0]  = {new chosen color}
    $lnk.Save()
)

I have not tested this code, but based on the above you should be able to get it working. The script Get-Link.ps1 has been around for many years, so should now be in a stable state.

1
  • This answer was helpful to me, but I had to change {new chosen color} to [System.Drawing.Color]::FromArgb(255, 42, 241, 165). Commented May 31, 2022 at 8:52
2

Unfortunately powershell.exe doesn't have flags to specify a color on start that you could add to a .lnk. The target you pinned is likely running cmd.exe (which starts black) to launch a powershell shell.

You can try to set registry settings in HKCU:\Console (from this answer). This is what the color picker in your screenshot does:

Set-Location HKCU:\Console 
New-ItemProperty . ColorTable00 -type DWORD -value 0x00562401 
New-ItemProperty . ColorTable07 -type DWORD -value 0x00f0edee 

Or add items to your profile script that gets loaded on PS start:

# print path to profile script
$profile 

# set example console colors in profile .ps1
[console]::backgroundcolor = "darkmagenta"
[console]::foregroundcolor = "darkyellow"
1
  • Your answer is good but it isn't what I wanted, as it writes the value to registry instead of writing the information to the .lnk file itself. Commented Jan 29, 2021 at 16:30
0

To change the background color what I would try to do is:

  1. run cmd
  2. issue color XX command - it is possible via registry to issue one command before the user will be able to issue his own commands
  3. call powershell from within cmd

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .