title: Create multiple shortcuts in different folders based on file name
body: <p>Basically, we have a lot of .mht files (generated periodically with MicroStrategy) and we need to serve these files on different folders to apply security. To avoid space consumption, we thought of creating shortcuts on several folders.</p>
  
  <p>The files all start with groupings of 4 digits and an underscore (e.g. 0001_0041, 0001_0043, etc.).</p>
  
  <p>This is what I have now:</p>
  
  <pre><code>Get-Childitem -Path "C:\Users\Max\Google Drive\Portal\Mermaid" -Recurse -Include "0002*.mht"
  Foreach-Object {
    $ruta = Get-Childitem -Path "C:\Users\Max\Google Drive\Portal\Mermaid" -Recurse -Include "0002*.mht"
    $nombre = Get-Childitem -Name "C:\Users\Max\Google Drive\Portal\Mermaid" -Recurse -Include "0002*.mht"
    $ncorto = $nombre | ForEach-Object {$nombre.Substring(0,9)}
    $container = "C:\Users\Max\Google Drive\Portal\Mermaid\MHT_Shortcuts\$ncorto"
    if (!(Test-Path $container)) {
      New-Item -ItemType directory -Path $container | Out-Null
    }
    $TargetFile = $ruta
    $ShortcutFile = "$container\$nombre.lnk"
    $WScriptShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
    $Shortcut.TargetPath = $TargetFile
    $Shortcut.Save()
  }
  </code></pre>
  
  <p>This works as long as there is only one file starting with 0002. However, they keep historic files that have the same name with timestamp.</p>
  
  <p>If there are multiple files that start with 0002 (there will be as they keep historic versions of these files), I get the error:</p>
  
  <pre>Exception setting "TargetPath": "The parameter is incorrect. (Exception from
  HRESULT: 0x80070057 (E_INVALIDARG))"
  En C:\Users\Max\Desktop\MK_LNK_V01.ps1: 25 Character: 7
  +                         $Shortcut.TargetPath = $TargetFile
  +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
      + FullyQualifiedErrorId : CatchFromBaseAdapterSetValueTI</pre>
  
  <p>Sorry if this is a duplicate, I couldn't find a post about this specific issue.</p>