Powershell选择包含特定字符串的最新文件

时间:2017-07-12 08:51:10

标签: powershell

我在powershell编写了一个代码,用于选择目录中的最新文件。

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

但是,我需要选择名称中包含特定字符串的最新文件。我怎样才能调整我的代码才能做到这一点?

4 个答案:

答案 0 :(得分:1)

@Michael Hoffmann

喜欢这个吗?

$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

答案 1 :(得分:1)

我用它来工作:

$filterIRP1064="IRP_1064*"
$latest1064 = Get-ChildItem -Path $dir -Filter $filterIRP1064 | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest1064.name

答案 2 :(得分:0)

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

使用此选项可获取包含字符串的所有文件。之后选择最新的一个。

答案 3 :(得分:0)

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

这应该有用......