我尝试过
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1
但是$latest1
提供空白输出。
尝试更改或删除对象过滤器
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer -eq $false } | sort CreationTime -desc | select -f 1
$latest1 = gci $path -Include *.zip| | sort CreationTime -desc | select -f 1
Note:您没有指定您的情况下的$path
是什么,所以我的假设是它的格式为C:\path\to\folder\
。
请参阅help for Get-ChildItem
。它说:
仅当命令包含项目的内容(例如,C:\ Windows *,其中通配符指定C:\ Windows目录的内容时,Include参数才有效)。>>
在示例4中,您可以确切地看到此问题:
Get-ChildItem
因此,要更改的第一件事是在路径末尾包含
# When using the -Include parameter, if you do not include an asterisk in the path # the command returns no output. Get-ChildItem -Path C:\Test\ -Include *.txt # Should be Get-ChildItem -Path C:\Test\* -Include *.txt
。接下来的事情是不要为*
过滤,因为它仅返回目录,并且PSIsContainer -eq $true
文件不是目录。
通常,当您逐个运行cmdlet并查看它们是否提供正确的输出时,可以更轻松地进行故障排除。例如,从您的代码中:
.zip
使用这种方法,您会注意到第一个cmdlet没有给您正确的结果。