Get-Childitem for files with similar names RRS feed

  • Question

  • Is it possible to use something like get-childitem to get files with similar names, then only keep the latest two versions by date?

    I know this can be done if they were in individual folders, but all files are in the same folder.

    Example files below in B:\backups. The files have same name followed by the date.







    Thursday, March 21, 2019 4:11 PM

All replies

  • Define "similar".  What can you say that is the same about all of the files you want and different from all other files?


    Thursday, March 21, 2019 5:44 PM
  • One part of your question seems easy enough -- use a simple wildcard.

    Get-ChildItem b:\backups\backup*

    Keeping only two of the results is also easy enough, just use "Select -last 2" or "Select -first 2".

    However, getting the names into "date" order is more difficult if "by date" you mean that part of the file name that looks like a date. If you were to use the files CreationTime property instead, the task becomes a lot easier (and accurate). For example:

    Get-ChildItem b:\backups\backup* | sort -property CreationTime | Select -last 2

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, March 21, 2019 6:33 PM
  • Hi

    Thanks for the response.

    Im not sure I can use a wildcard. If you look at my example files in the post, there may be about 5 files that start with backup, then 5 files that start with test, all the backup files have the date after the name as in my example above, there are loads of these in the folder.



    Tuesday, March 26, 2019 9:47 AM
  • What is the extension on the files?  Is it ".bak"?


    Tuesday, March 26, 2019 2:49 PM
  • Yes, they are all .bak files.
    Tuesday, March 26, 2019 3:01 PM
  • Then use that to filter:



    Tuesday, March 26, 2019 3:18 PM
  • In that case, why concern yourself with the root name of the file?

    A wildcard will still work, just not the one I proposed. JRV's given you the one to substitue for the one in my example.

    Even if you have several different extensions you can still deal with this by using the "-Include" parameter and supply a list.

    get-childitem -file -include *.bak,*.bkp,*.old

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, March 26, 2019 3:31 PM
  • Filtering on the *.bak will get all the files in the backup folder.

    I need to be able to delete all but the last two files for each file name.

    Taking the example files from my original post, what i am asking is, is it possible to use get-childitem to sort and get the files so only the last two backups for each filename are kept as shown below.

    backup20302019 - Delete

    backup21302019 - Delete

    backup22032019 - Keep

    backup23032019 - Keep

    test21032019 - Delete

    test22032019 - keep

    test23032019 -Keep

    Tuesday, March 26, 2019 3:39 PM
  • Get-ChildItem *.bak | sort LastWriteTime | select -last 2


    • Edited by jrv Tuesday, March 26, 2019 3:49 PM
    Tuesday, March 26, 2019 3:48 PM
  • This would only return the below two files from the above files examples.



    Whereas i would like to keep the last two versions of "test" and "backup" files as shown below

    test22032019 - keep

    test23032019 -keep

    backup22032019 - Keep

    backup23032019 - Keep

    Tuesday, March 26, 2019 4:37 PM
  • You are changing the request.  We cannot design a custom solution.  You need to start by learning PowerShell and the Windows file system.  Once you have that you can easily test and build almost anything you need with little help.  The reason PS is so popular is that it can be learned by anyone willing to put in the effort.

    I will give you a hint for a starter.

    Get-ChildItem *.bak | Group-Object Basename


    Tuesday, March 26, 2019 4:49 PM
  • I have not changed the request, my original post specified i know get-childitem could be used if files were in different folders, but wasn't sure how to do this if files were in the same folder.
    Tuesday, March 26, 2019 4:53 PM
  • Try writing some code to show us what you are trying to do and what errors or issues you are having.

    What have you tried and what doesn't work?


    Tuesday, March 26, 2019 5:07 PM
  • Well, the answer is "not easily". But try this:

    $dir = "c:\temp"
    $ext = "bak"
    $dateformat = "ddmmyyyy"          # doesn't really matter what it is, just how long it is
    $datelength = $dateformat.Length
    $h = @{}
    # get the set of "similar" names
    #   in this case, the similarity is that they all end with eight digit "dates"
    #   AND they all have the same file extension
    Get-ChildItem ($dir + "\*." + $ext) | where {$_.basename -match "\d{$datelength}$"} |foreach {
        $k = $_.Basename.Substring(0, ($_.BaseName.Length - $datelength))
        $h.($k) = ""
    # for each "similar" name (reduced by removing the eight digit "date")
    # get just the last two "most recent" files
    $h.keys | foreach {
        Get-ChildItem ($dir + "\" + $_ + "*." + $ext) | sort -property CreationTime | Select -last 2

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, March 26, 2019 6:42 PM