6

I have written a code in powershell that selects the most recent file in a directory.

$first = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
$first.name

However, I need to select the most recent file containing a specific string in the name. How can I adapt my code in order to do this?

4 Answers 4

4

I got it to work using this:

$filterIRP1064="IRP_1064*"
$latest1064 = Get-ChildItem -Path $dir -Filter $filterIRP1064 | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest1064.name
Sign up to request clarification or add additional context in comments.

Comments

1

@Michael Hoffmann

Like this?

$first = Get-ChildItem -recurse | Select-String -pattern "stringhere" | group path | select name
Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
$first.name

Comments

0
Get-ChildItem -recurse | Select-String -pattern "stringhere" | group path | select name

Use this to get all the files containing your string. Select the most recent one afterwards.

Comments

0
Get-ChildItem -path $dir | Select-String -pattern "stringhere" | group path | Sort-Object CreationTime -Descending | Select-Object -First 1 | select name

This should work...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.