Create shortcuts from a text file with powershell

Published by Marian Galie-Andriescu on

At some point I needed to create a lot of shortcuts based on file names stored in a text file.
This is how you can do it with PowerShell with a couple of lines of code:

#########################################################################################################
# Marian Galie-Andriescu        marian.galie.andriescu@gmail.com                                        #
# Create shortcuts to file names stored in a text file                                                  #
#########################################################################################################

#Text file containing the files names, full file name, one file name per line:
$inputFile = "c:\Users\MGA\MyDocuments\ListOfFiles.txt"
#full directory name where to create the shortcuts:
$targetDirectory = "c:\Users\MGA\MyDocuments\shortcuts\"

foreach($line in Get-Content $inputFile) 
{
  $filename = [System.IO.Path]::GetFileName($line); #get file name without directory
  #create shortcut
  $s=(New-Object -COM WScript.Shell).CreateShortcut($targetDirectory + $filename + '.lnk');
  $s.TargetPath = $line;
  $s.Save();
}

I got the list of file names using the baregrep tool, which is very handy Windows tool for searching in text files using regex expressions and extracting the results similar to the grep command in Linux:

https://www.baremetalsoft.com/baregrep/

For example if you are looking for all methods with a signature similar to CreateStoredProcedure_ABC() you can use regex: CreateStoredProcedure_([0-9_A-Za-z]*)\(

Categories: PowerShell

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.