# Shortcut Errors creating link in different folder than source

**URL:** <https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365>\
**Category:** PowerShell Help\
**Created:** [April 28, 2021, 12:45pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365 "2021-04-28T12:45:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShaunBlake](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/shaunblake/32/1273_2.png) [@ShaunBlake](https://forums.powershell.org/u/ShaunBlake)\
**Post date:** [April 28, 2021, 12:45pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/1 "2021-04-28T12:45:05Z")

</div>

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:

```auto
# 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!

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 28, 2021, 1:04pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/2 "2021-04-28T13:04:25Z")

</div>

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:

```auto
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()
}

```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 28, 2021, 1:06pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/3 "2021-04-28T13:06:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 28, 2021, 1:28pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/4 "2021-04-28T13:28:55Z")

</div>

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

> **[New-Item (Microsoft.PowerShell.Management) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-7.2&viewFallbackFrom=powershell-7.1)**
>
> The New-Item cmdlet creates a new item and sets its value. The types of items that can be created depend on the location of the item. For example, in the file system, New-Item creates files and folders. In the registry, New-Item creates registry keys...

I’d give it a shot. 😃

---

<div class="post-metadata">

**Author:** ![ShaunBlake](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/shaunblake/32/1273_2.png) [@ShaunBlake](https://forums.powershell.org/u/ShaunBlake)\
**Post date:** [April 28, 2021, 1:43pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/5 "2021-04-28T13:43:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ShaunBlake](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/shaunblake/32/1273_2.png) [@ShaunBlake](https://forums.powershell.org/u/ShaunBlake)\
**Post date:** [April 28, 2021, 1:50pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/6 "2021-04-28T13:50:37Z")

</div>

> [@rob-simmers](#):
>
> Normally, you wouldn’t create a link directly to the HTML either,

@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!

---

<div class="post-metadata">

**Author:** ![ShaunBlake](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/shaunblake/32/1273_2.png) [@ShaunBlake](https://forums.powershell.org/u/ShaunBlake)\
**Post date:** [April 28, 2021, 2:03pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/7 "2021-04-28T14:03:28Z")

</div>

> [@Olaf](#):
>
> I’d give it a shot.

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 28, 2021, 2:37pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/8 "2021-04-28T14:37:32Z")

</div>

> [@ShaunBlake](#):
>
> but my efforts were far worse than with New-Object.

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

```auto
$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
}

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 28, 2021, 2:49pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/9 "2021-04-28T14:49:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 28, 2021, 2:58pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/10 "2021-04-28T14:58:17Z")

</div>

> [@matt-bloomfield](#):
>
> it did require an elevated console.

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

---

<div class="post-metadata">

**Author:** ![ShaunBlake](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/shaunblake/32/1273_2.png) [@ShaunBlake](https://forums.powershell.org/u/ShaunBlake)\
**Post date:** [April 28, 2021, 5:16pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/11 "2021-04-28T17:16:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 28, 2021, 5:57pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/12 "2021-04-28T17:57:27Z")

</div>

> [@ShaunBlake](#):
>
> I’m not sure how to succinctly clear up the puzzle.

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

> [@ShaunBlake](#):
>
> Perhaps part of my difficulty is due to refusing to run code as Admin or in an elevated console.

How do you install Software on your system? 😉

---

<div class="post-metadata">

**Author:** ![ShaunBlake](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/shaunblake/32/1273_2.png) [@ShaunBlake](https://forums.powershell.org/u/ShaunBlake)\
**Post date:** [April 28, 2021, 9:54pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/13 "2021-04-28T21:54:59Z")

</div>

> [@Olaf](#):
>
> 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. 😉

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

> [@Olaf](#):
>
> How do you install Software on your system?

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 28, 2021, 10:56pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/14 "2021-04-28T22:56:52Z")

</div>

> [@ShaunBlake](#):
>
> … Eventually I hope to develop a Word or PDF document cataloging …

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

> [@ShaunBlake](#):
>
> Apps that require Admin rights I usually eschew unless from a trusted source.

This wasn’t actually meant as a question. This was meant to be more food for thought. 😉  
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!! 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:26pm UTC](https://forums.powershell.org/t/shortcut-errors-creating-link-in-different-folder-than-source/16365/15 "2024-05-16T20:26:30Z")

</div>


