Shortcut Errors creating link in different folder than source

My objective is a folder of “Shortcuts” in Windows that point to HTMLfiles in a different folder (same drive; same user account). I’m only able to create viable links when creating them in the same folder as the target file. My (best effort) code fails to populate the “Start In” folder in Properties.

Here’s my code:

# Shortcuts for HTML files b1.ps1
<#
 Objective:
    Creating links for all files in a directory with PowerShell
 
 Directory:
    $Home\Bios
 Target:
    All '.html' files
 Destination:
    $Home\BioLinks
#>

# Set Vars
$SourcePath = "$Home\TC_temp\Bios"
$DestPath = "$Home\TC_temp\BioLinks"

# Loop through files in directory
$files = Get-ChildItem $SourcePath *.html

# Foreach-Object{
foreach ($file in $files) {
    # Create Shortcuts
    $WshShell = New-Object -comObject WScript.Shell
    $TargetPath = Join-Path $DestPath $file
    $Shortcut = $WshShell.CreateShortcut($TargetPath+'.lnk')
    $Shortcut.Arguments = "$SourcePath"
    $Shortcut.TargetPath = $TargetPath
    $Shortcut.Save()
}

Please explain what changes need to be made to this code. And any suggestions (or caveats) would be appreciated!

You seem to be confusing the destination path (the path where the shortcut file will end up) with the target path (the location that the shortcut should point to). Try the following for your foreach loop:

foreach ($file in $files) {
    # Create Shortcuts
    $WshShell = New-Object -comObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut("$DestPath\$($file.name).lnk")
    $Shortcut.TargetPath = $file.FullName
    $Shortcut.WorkingDirectory = $file.DirectoryName    # if you want 'Start in:' populated
    $Shortcut.Save()
}
2 Likes

Normally, you wouldn’t create a link directly to the HTML either, you would create a link the application that you want to open the file and the path to the HTML is the argument.

I’m not sure since when but at least since PowerShell version 5.1 PowerShell is able to create symbolic links natively with the cmdlet New-Item. :wink:

I’d give it a shot. :smiley:

1 Like

@matt-bloomfield, seem is a huge understatement (and I appreciate your gentle observation): when I read Microsoft’s synopses of statements, my eyes cross.

Your corrections give me exactly what I hoped to achieve, and somehow I overlooked “.WorkingDirectory” in my searches.

@rob-simmers, that had not occurred to me since the OS is configured to use a browser. Experimenting with your suggestion will go on my list of next things to try. Thanks a BUNCH, it’s really inspiring!

Thanks, @Olaf; I’m on Win 10, and had PS 5.1 and I did try symbolic links but my efforts were far worse than with New-Object. When I have cleaned up/organized my biographies I plan to learn how to create them… but PS 7 might offer even better options.

I don’t get it. What’s the problem? I tested it in both versions and it worked just as expected.

$SourcePath = "$Home\TC_temp\Bios"
$DestPath = "$Home\TC_temp\BioLinks"
Get-ChildItem -Path $SourcePath -File |
ForEach-Object {
    $LinkName = Join-Path -Path $DestPath -ChildPath $_.Name
    New-Item -Path $LinkName -ItemType SymbolicLink -Value $_.FullName
}

Testing that here, and while it works, it did require an elevated console. The old, non-PS method works for standard users.

It does indeed. And I actually don’t understand why. I just wanted to show that it’s actually really straight forward and I couldn’t understand Shauns complains. :wink:

I’m not sure how to succinctly clear up the puzzle. Perhaps part of my difficulty is due to refusing to run code as Admin or in an elevated console. (And in frustration, I did open one when some code wouldn’t run but the subsequent errors made me even more confused and frustrated.) But the basic problem is that the descriptions often lead me to get the code backwards—as in the case of switching destination and target paths.

Honestly, I don’t fully understand and appreciate the underlying framework, so understanding the com objects, parameters, and requirements is problematic. And when I refer to Microsoft’s description of them I usually don’t have an “ah-ha!” experience. :nauseated_face:

Well … you explained what you want to achieve but you didn’t explain why or what’s the big picture behind it. There might be a another even better way but we’ll never know. :wink:

How do you install Software on your system? :wink:

Aw, for pity’s sake! I explained howcome but it got wordy; I cut it out. My objective is to make a TOC-type list of biographies, and one of reviews I’ve published. Eventually I hope to develop a Word or PDF document cataloging a particular type, with hyperlinks to the document/file wherever it is. Currently I’m working on files on my local HD, but reviews are on a Cloud system and a lot of material is on a NAS. (The NAS I access as mapped/mounted drives.)

Actually, I am pretty skittish about installing software, so I usually install it locally. Apps that require Admin rights I usually eschew unless from a trusted source. However (I might have mentioned this) I did make one attempt at getting a version of this to run when it objected to a lowly User trying to perform such wizardry … the errors that generated were so numerous and alarming that I didn’t even consider trying to understand what was missing/malformed.

1 Like

OK, if the help you’ve got here helps on the way to that that’s great. :+1:t4: :wink: I just don’t know if PowerShell is actually the right tool for this kind of task.

This wasn’t actually meant as a question. This was meant to be more food for thought. :wink:
If you have to have admin rights for certain tasks you need to run it with admin rights. And I think you should consider yourself as a trusted source!! :wink:

1 Like