How to create a shortcut to a folder with PowerShell and Intune
I've been battling this for the past several hours, searching to no avail; there are a couple TechNet posts (e.g. https://social.technet.microsoft.com/Forums/Windows/en-US/3a6c58b2-840f-42e3-8661-69cca1429566/is-there-a-way-to-set-target-type-when-creating-a-shortcut-to-network-folder?forum=winserverpowershell) that don't answer the question and tell the questioner to ask in a different forum, but maddeningly there's neither a link to the question asked in that forum nor a hit while searching; I would reply there, but TechNet is of course now archived).
For posterity, here's my summary. While alternatives suggestions some offer are appreciated:
- PowerShell inexplicably has no direct way to create a shortcut... it would save so much time if it did. . It can directly create a symbolic link, but 1) that requires admin rights, and 2) symbolic links behave differently.
- Group Policy Preferences are great--but only if your machines are in office or routinely on a VPN.
- PowerShell can use WScript.Shell to create shortcuts, but if you're trying to create a shortcut to a network folder, setting the TargetPath to that folder only works if the computer actually can reach that target folder when the script runs--in other words, the same issue as with Group Policy Preferences. PowerShell will create the shortcut, but the Target Type frustratingly will be a File rather than a File Folder (I found no info online how to control that; trailing slash or not doesn't matter).
- The behavior of running these code suggestions varies depending whether you run this interactively or as a script.
There are many examples of scripts using WScript.Shell, but when someone asks about how to create a shortcut to a folder, the answers always seem to be either dodges ("Why are you doing that? Use symbolic links" or "Use Group Policy Preferences") or irrelevant examples of how to create a shortcut to a file.
Here's what I found does work: use Explorer.exe with an argument of the target path. By example:
$shell = New-Object -comObject WScript.Shell
$shortcut = $shell.CreateShortcut("[Target location of shortcut\shortcut name.lnk]")
$shortcut.TargetPath = "C:\Windows\Explorer.exe"
$shortcut.Arguments = """\\machine\share\folder"""
Hopefully this proves useful for someone else and saves some time!