Use of Wild Cards in PS Script?

Dear Experts,

I currently use a reasonably simple PS script to delete any files older than 7 days.

These files originate from a number of different sources but ultimately reside in the same folder where I run my script.

They are prefixed with a client ID, followed by a sequential identifier...



I now have a requirement to reduce the retention of some of the files down to 1 day. For example, files from Company A & B should retain files up to 7 days old, but Company C should only retain files up to 1 day old.

The script I currently use is shown below, and I was wondering if the best way to tackle this problem is to use wildcards. Do you agree? And if so, could you help me out with the syntax?

$limit = (Get-Date).AddDays(-7)
$path = "C:\ExtractedFiles"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

Open in new window

I should add that I'm a simpleton when it comes to PS :-)
Andrew MellorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Wildcards are only a viable way, if your client ID has a pattern. Otherwise triage the files. E.g.


$companies = @( "companyA", "companyB" )
$limit1 = (Get-Date).AddDays(-1)
$limit7 = (Get-Date).AddDays(-7)
$folder = "C:\Temp"

Function InCompanies() {
    Param ([string] $filename)
    $result = $False
    ForEach ($company in $companies) {
        If ($filename.StartsWith($company)) {
            $result = $True

    return $result

Get-ChildItem -Path $folder -Force |
    Where-Object {
        !$_.PSIsContainer -and $_.CreationTime -lt $limit7
    } |
    Remove-Item -Force

Get-ChildItem -Path $folder -Force |
    Where-Object {
        !$_.PSIsContainer -and $_.CreationTime -lt $limit1 -and (InCompanies $_.BaseName)
    } |
    Remove-Item -Force

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andrew MellorAuthor Commented:
Many thanks for this.

The files that are exported to my folder, do begin with a company identifier.

So in your script, am I correct in assuming that the script will remove all files older than 7 days, AND THEN, remove any files beginning with CompanyA or CompanyB that are older than 1 day?

Apologies for the questions.


Andrew MellorAuthor Commented:
I have just tested, and this works perfectly for me. Thanks so much.


Andrew MellorAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.