bookends
bookends

Reputation: 131

creating a shortcut on a remote desktop, but the shortcut is created with a "file" instead of "file folder" as the "target type"

I'm trying to create a shortcut on a remote desktop in the domain here, and I'm a domain admin. If I run the following code directly on the target machine, the shortcut can be created and is able to lead me to the target path.

$shortcutpath3 =  "c:\Users\Public\Desktop\Shortcuts to Test Custom\VV 1211 -TC.lnk"
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = "\\machine\testcustom\"
$Shortcut3.Save()

I saved this script as test.ps1, run it with folloing code on a different mahchine. The code ends without any errors/warings, and the shortcut is created on the target machine with the propeties i specified. But it cannot lead me to the target place, it actually ask me to pick a program to open that file. I compared the properties of the 2 shortcuts, and found that the "target type" of the broken shortcut is "file" while it is "file folder" for a good shortcut.

Invoke-Command -ComputerName TARGETSERVER -FilePath test.ps1

Any idea how i can fix this? And why is this happening? Thank!!!

Upvotes: 1

Views: 5699

Answers (3)

bluvg
bluvg

Reputation: 1

I've been battling this for the past several hours, Googling to no avail. For posterity, here's my summary. While alternatives suggestions are appreciated:

  • PowerShell inexplicably has no direct way to create a shortcut. It can create symbolic link, but 1) it requires admin rights, and 2) it behaves differently.
  • Group Policy Preferences are great--but only if your machines are in office or routinely on a VPN.
  • 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 it when the script runs, i.e. same issue as with Group Policy Preferences. PowerShell will create the shortcut, but the Target Type will be a File rather than a File Folder (and 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.

Here's what I found does work:

  • Set the $shortcut.TargetPath to "C:\Windows\Explorer.exe"
  • Add a line: $shortcut.Arguments = """Target folder""" (the """ are needed as an escape character for the shortcut to show as C:\Windows\Explorer.exe "Target folder")

So for your example above, the following should work (assuming permissions to c:\Users\Public\Desktop):

$shortcutpath3 =  "c:\Users\Public\Desktop\Shortcuts to Test Custom\VV 1211 -TC.lnk"
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = "C:\Windows\Explorer.exe"
$shortcut.Arguments = """\\machine\testcustom\"""
$Shortcut3.Save()

Upvotes: 0

user2888490
user2888490

Reputation: 11

I had the same problem and and I used Get-Item to make it work. Try this:

$targetPath = Get-Item("\\machine\testcustom\")
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = $targetPath.FullName
$Shortcut3.Save()

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Since you're a domain admin I'd strongly recommend doing this with a Group Policy Preference. You can restrict shortcut creation to particular users/groups/computers/etc. via item-level targeting.

Upvotes: 0

Related Questions