Search code examples
powershellpathalphanumeric

Powershell: Get path of last alphanumerically sorted file in a directory


I have a directory with the following files:

  1. Program-3.0.79.J1231.exe
  2. Program-3.0.79.J1230.exe
  3. Program-3.0.79.J1229.exe
  4. Program-3.0.79.J1228.exe
  5. etc...

These files will keep updating every few weeks. For example Program-3.0.79.J1232.exe will automatically be put in this directory in a week or two

Using Power Shell, how would I easily go about getting the path of the latest (NOT WITH last modified) file added. Preferably via alphanumeric sorting


Solution

  • It's fairly simple:

    ((Gci 'C:\Myfolder' | sort-object name)[-1]).fullname

    The [-1] index means "last item in the array".