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

10
  • 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. Jul 16, 2015 at 16:56
  • No brianist. That post discusses creating a file. Not a directory.
    – mreff555
    Jul 16, 2015 at 16:59
  • 1
    @mreff555 Are you sure that $root_path points to existing directory? BTW use @UserName to notify user of your comment. Jul 16, 2015 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, 2015 at 17:29

3 Answers 3

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

New-Item Documentation

3
  • What's the benefit of using New-Item over New-Object, as done in the accepted answer from four years ago? Sep 27, 2021 at 0:19
  • 1
    For me. Easier to read. Sep 28, 2021 at 1:34
  • This requires admin privileges to execute
    – Narish
    Aug 31, 2022 at 20:17
4

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
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.

1
  • 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 1, 2019 at 23:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.