PowerShell – parse beginning characters out of filenames, so they can be compared properly

Sample of file names in our naming convention using PowerShell:

  • SYSTEM_20201118_file1.txt
  • SYSTEM_20201118_file2.csv
  • SYSTEM_20201118_file3_and_file4.txt
  • SYSTEM_20201118_file3_and_file4.txt

We use this convention and it worked well as simply “SYSTEM_file1.txt” before we added the date. If we include the date in there, our script will only compare them if the date matches (which makes sense). This works mostly because the filenames are typically the same in the before and after. However, sometimes the dates will change and we simply have to manually rename all the files in the before, and then it works properly. We want to alleviate this manual work for others.

Is there a way I can take the $before and $after and redefine them parsing out the first XX characters to only compare based on the name after the second _ character? The filenames, filename lengths, and extensions vary so I need to start from the beginning and not the end.

I know I may need to use $SYSTEM.Length in the first part of the variable but I’m not sure how to structure the rest of it.

Thank You!