Search code examples
powershellshortcut-file

Create multiple shortcuts in different folders based on file name


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.

The files all start with groupings of 4 digits and an underscore (e.g. 0001_0041, 0001_0043, etc.).

This is what I have now:

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

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.

If there are multiple files that start with 0002 (there will be as they keep historic versions of these files), I get the error:

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

Sorry if this is a duplicate, I couldn't find a post about this specific issue.


Solution

  • Here's a short example of how to create .lnk files for all files in a directory:

    $shortcutPath = "C:\TextFiles"
    $wshShell = New-Object -ComObject "WScript.Shell"
    Get-ChildItem (Join-Path $shortcutPath "*.txt") | ForEach-Object {
      $lnkFilename = Join-Path $shortcutPath ("{0}.lnk" -f [IO.Path]::GetFilenameWithoutExtension($_.FullName))
      $shortcut = $wshShell.CreateShortcut($lnkFilename)
      $shortcut.TargetPath = $_.FullName
      $shortcut.Save()
    }
    

    Of course you will need to modify this example to suit your needs.