Find file by the date in name

Hello everyone,
I have few files with names in following format:

Name_mmddyy_hhmmss.bak

Files are created every day and part of file name “mmddyy” shows day of creation. What i would like to do is to delete files with the name different then today.
I can’t use following:

Get-ChildItem -Path "C:\path\to\folder" -File -Recurse | 
    Where-Object { 
                   ($_.BaseName -notlike '*%mmddyy%*')} |
                   Remove-Item -WhatIf

because i would like to do it automatically so the part ‘%mmddyy%’ should be generated by function. Any ideas?

Hi, welcome back :wave:

If I’ve understood correctly, something like this:

$today = Get-Date -Format 'MMddyy'

Get-ChildItem -Path "C:\path\to\folder" -File -Recurse | 
    Where-Object { $_.BaseName -notlike "*$today*" } |
        Remove-Item -WhatIf

How about

Get-Date -Format 'MMddyy'

?

And BTW: you should omit the % signs in your pattern. :wink: