4

I'm writing my first powershell script and I'm having a little trouble.

Up to this point my system creates a directory tree and populates it with files. The final step is to put a shortcut on my desktop.

I've come up with the code below:

$ShortcutFile = "$home\Desktop\" + $protocol + ".lnk"
If ( (test-path -path "$ShortcutFile") -ne $true)
{
    $WScriptShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
    $Shortcut.TargetPath = $root_path
    $Shortcut.Save()
}

This doesn't work as I'm sure any experienced powershell user knows. A file is created rather than a directory. I imagine the correct way to fix this is to change one of the object members in WScript.Shell which control's the file type. I have had no luck locating any resources on how to do this specifically, or any other way to go about doing it. I found the API on the MSDN website but there where only a few members listed. There must be more. What is the best way to accomplish this?

Thanks

mreff555
  • 1,049
  • 1
  • 11
  • 21
  • possible duplicate of [Shortcut that points to folder named the current date. YYYY\_MM\_DD Format](http://stackoverflow.com/questions/16469899/shortcut-that-points-to-folder-named-the-current-date-yyyy-mm-dd-format) – briantist Jul 16 '15 at 16:51
  • 1
    *A file is created rather than a directory.* Not sure what you mean by that, shortcut is a file even if it point to directory. I does not see any problem with your code. If I define `$protocol='SomeName';$root_path='C:\'`, than your code work without any problems. – user4003407 Jul 16 '15 at 16:56
  • No brianist. That post discusses creating a file. Not a directory. – mreff555 Jul 16 '15 at 16:59
  • When I use that code I get a file that windows doesn't know how to open. When I look at it under properties the type is listed as a file. If you make a shortcut from a folder the type is listed as"file folder" – mreff555 Jul 16 '15 at 17:01
  • 1
    @mreff555 Are you sure that `$root_path` points to existing directory? BTW use `@UserName` to notify user of your comment. – user4003407 Jul 16 '15 at 17:25
  • 1
    Your code works perfectly fine for me on Windows 7/PS 4.0. What value does `$ShortcutFile` have? What does `Test-Path $ShortcutFile -IsValid` return? Are you expecting shortcuts to be navigable from the command line like NTFS junctions are? – Bacon Bits Jul 16 '15 at 17:29
  • Yes I'm sure $root_path points to a directory. I have attached my entire source to the question. – mreff555 Jul 16 '15 at 17:47
  • On Win7, PS5, your code works as PetSerAl said, just by supplying values for $protocol and $root_path. The only thing out of the ordinary I see is that the icon is not a folder icon. But looking at the properties of the .lnk file everything else is as expected and if I double-click it takes me to $root_path. Can you tell us what the Type column says in explorer for the .lnk file created on your PC? – Eric Walker Jul 16 '15 at 20:04
  • I can confirm what @mreff555 is saying,I have the same issue, while creating shortcuts on server remotely. If I create shortcut to C:\ it works fine but fails for any other folder. "file" looking like shortcut is created - checking properties and it looks like shortcut, everything is fine there. What is going on ? Did you ever figure it out ? – bart Jun 08 '17 at 13:25
  • No I was never able to resolve the issue. – mreff555 Jun 08 '17 at 13:40

3 Answers3

5
New-Item -itemtype symboliclink -Path "PathWhereYouWantToPutShortcut" -name "NameOfShortcut" -value "PathOfWhatYourTryingToLinkTo"

New-Item Documentation

Bulgypayload
  • 51
  • 1
  • 3
3

Assuming that you mean the shortcut type is a File rather than a File folder then a workaround is to make an Application launcher instead, which always works.

I originally found this solution here.

    $wsshell = New-Object -ComObject WScript.Shell
    $lnk = $wsshell.CreateShortcut($ShortcutFile)
    $lnk.WindowStyle = 1
    $lnk.TargetPath = "explorer.exe"
    $lnk.Arguments = $TargetPath
    $lnk.IconLocation = "explorer.exe,0"
    $lnk.save() # This will overwrite the previous link if it existed
kalebo
  • 332
  • 5
  • 9
0

I experienced this when creating a shortcut to a directory that didn't exist. If I simply created the directory ahead of time, then the shortcut worked correctly.

  • Yeah, that’s what happened. The thing is that it was done intentionally. I was trying to use it like a Linux symbolic link. – mreff555 Feb 01 '19 at 23:05