5

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
  • possible duplicate of Shortcut that points to folder named the current date. YYYY_MM_DD Format Commented Jul 16, 2015 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. Commented Jul 16, 2015 at 16:56
  • No brianist. That post discusses creating a file. Not a directory. Commented 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. Commented 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? Commented Jul 16, 2015 at 17:29

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

Comments

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

New-Item Documentation

4 Comments

What's the benefit of using New-Item over New-Object, as done in the accepted answer from four years ago?
For me. Easier to read.
This requires admin privileges to execute
That create a symbolic link, not a shortcut
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 Comment

Yeah, that’s what happened. The thing is that it was done intentionally. I was trying to use it like a Linux symbolic link.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.