Thus, my question is: How do I create a shortcut that is equivalent to the shortcut created when using the right-click option Create Shortcut
on a printer on the devices and printers page
with powershell?
CodePudding user response:
The correct shortcut does not point to rundll32.exe, it points to the printer in the shell namespace. This target is an item id list, not a filesystem path.
I don't know the native way to do this in Powershell. With P/invoke it would be SHParseDisplayName
IShellLink::SetIDList
.
You would probably want to go the reverse way first; on a correct link, get its id list and call SHGetNameFromIDList(...,SIGDN_DESKTOPABSOLUTEPARSING,...)
. The returned string would look something like ::{GUIDHERE}\::{ANOTHERGUID}\MyPrinterName
.
CodePudding user response:
Thanks to Anders for bringing me onto the right track.
You can achieve it like this:
Get a list of all devices and printers:
$shell = New-Object -ComObject Shell.Application
$devices_and_printers = $shell.namespace("::{26EE0668-A00A-44D7-9371-BEB064C98683}\2\::{A8A91A66-3A7D-4424-8D24-04E180695C7A}")
$devices_and_printers.items() | select name,path
Create shortcut:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:userprofile\Desktop\My Printer.lnk")
$Shortcut.TargetPath = ($devices_and_printers.items() | where { $_.name -eq "My Printer Name" }).Path
$Shortcut.Save()