0

On my windows desktop I have a folder called Links with one file: Firefox.lnk

I manually set a hotkey "CTRL+SHIFT+F" for this shortcut and everything worked as expected.

But after a reboot my hotkey "CTRL+SHIFT+F" does not work any more.

Manually setting the hotkey back to "" and then back to "CTRL+SHIFT+F" makes it work again.

But planning to put more files into this folder - the manual solution was not an option - so my plan was to set all shortcut hotkeys to an empty string via a powershell script and then run the following script to set all hotekeys to "CTRL+SHIFT+FirstletterOfShortcutFileName".

The first script sets the hotkey back to "" (manual check) and after running the following script the hotkey of the firefox shortcut was set to "CTRL+SHIFT+F". But unfortunately the hotkey does not work. How can I make it work?

$lnkfiles = Get-ChildItem -Path "C:\Users\xxx\Desktop\Links\" -Filter *.lnk
foreach($lnkfile in $lnkfiles){
    $sh = New-Object -COM WScript.Shell
    $targetPath = $sh.CreateShortcut($lnkfile.FullName)
    $fileName = Split-Path $targetPath.FullName -leaf
    $firstLetter = $fileName[0]
    $targetPath.HotKey = "CTRL+SHIFT+" + $firstLetter
    $targetPath.Save()
}
4
  • I'd say that .HotKey works only for shortcuts on your desktop (does not in a subfolder of the desktop). Commented Oct 10, 2019 at 21:20
  • The hotkeys work when added manually - even in a subfolder. Commented Oct 12, 2019 at 15:22
  • Do the manually added hotkeys in a subfolder work even after computer restart? Commented Oct 12, 2019 at 18:12
  • Nope – just moved the shortcut to the desktop and after a restart the hotkey is not working Commented Oct 12, 2019 at 20:27

1 Answer 1

2

There's a few things wrong with your code, mainly you are setting the HotKey property on the target path instead of the link, and also you are using the Save() method on the target path instead of the link.

This should do what you want:

$rootPath = 'C:\Users\xxx\Desktop\Links'
$wshShell = New-Object -ComObject WScript.Shell

Get-ChildItem -Path $rootPath -Filter *.lnk | ForEach-Object {
    $shortcut        = $wshShell.CreateShortcut($_.FullName)
    $target          = [System.IO.Path]::GetFileName($shortcut.TargetPath)
    $shortcut.HotKey = 'CTRL+SHIFT+{0}' -f $target.Substring(0,1).ToUpperInvariant()
    $shortcut.Save()
    # cleanup this shortcut COM object after use
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut) | Out-Null
}

# clean up the WScript.Shell COM object after use
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wshShell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

P.S. I wouldn't do a combination of CTRL+SHIFT+.. because too many default hotkeys in Windows use that. Perhaps better do something with CTRL+ALT+..

Sign up to request clarification or add additional context in comments.

1 Comment

Your P.S. is the solution - now everything works! Manually I always typed CTRL+ALT but in my code it was CTRL+SHIFT - soooooo stupid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.