![](/img/trans.png)
[英]Powershell Get-childitem script not saves output on Windows 10 machines or when pushed remotely
[英]Windows10/Powershell: How to use -include parameter when using Get-Childitem?
使用“ -filter”時:
Get-ChildItem -file -filter "*.txt" | foreach-object { write-host $_.FullName
Get-ChildItem -file -filter "*.txt" | foreach-object { write-host $_.FullName
}
我得到了當前文件夾中的4個.txt文件的列表。
我嘗試使用“ -include”
Get-ChildItem -file -include *.txt | foreach-object { write-host $_.FullName }
Get-ChildItem -file -include *txt | foreach-object { write-host $_.FullName }
我什么也沒得到。 我嘗試使用和不使用“ -file”參數,都沒有區別。
我看過各種指南/示例(ss64.com/TechNet等),並且據說我做得對。
有什么想法我可能做錯了嗎? 謝謝!
從Get-ChildItem
的“ Get-Help
頁面:
-Include
參數僅在命令包含-Recurse
參數或路徑指向目錄內容(例如C:\\Windows*
,其中“*
”通配符指定C:\\Windows
目錄。
你會注意到,如果你指定你沒有得到一個語法錯誤-include
並沒有指定-recurse
盡管如此,無論它是從字面上未定義的事實。 您還將注意到,“ C:\\Windows*
不是“ C:\\Windows
目錄中的所有文件”的普通通配符表達式。 它是“ C:\\
目錄中所有以'Windows'開頭且可能具有或不具有擴展名的項目”的通配符表達式。 我不知道Get-ChildItem
的作者認為該參數應該做什么。 他們在記錄和實施它方面做得非常差。
因此,我避免使用-Include
參數,因為該文檔已損壞/記錄不良。 我不知道該怎么辦-Filter
不行。 我已經閱讀了有關其確切功能的文章。 它以某種方式“將值傳遞給基礎提供程序以在該級別進行過濾”。 我不知道為什么他們認為系統管理員會知道真正的含義。 我的理解是,在每個目錄項上調用DirectoryInfo.GetFiles()
在每個目錄項上調用DirectoryInfo.GetFiles('*.txt')
之間是有區別的,但是大多數系統管理員不會知道這意味着什么。 但是,它的行為如此奇怪以至於我不信任它,因此即使我大約95%地確定它會做什么……我仍然從不使用它。
相反,我只是管道到Where-Object
:
Get-ChildItem -file | Where-Object Extension -eq '.txt' | [...]
還要注意,在某些版本的PowerShell中, Get-ChildItem
被-LiteralPath
, -Recurse
和-Include
破壞了,而是返回了所有項目。
相比:
Get-ChildItem -LiteralPath $PSHOME *.exe -Recurse # works
Get-ChildItem -Path $PSHOME -Include *.exe -Recurse # works
Get-ChildItem -LiteralPath $PSHOME -Include *.exe -Recurse # does NOT work
v6 在這里報告的問題。
這些對我有用,無需遞歸:
Get-ChildItem -Path "C:\Users\Athom\Desktop\*.txt"
Get-ChildItem -Path ".\*.txt"
或者只需添加遞歸參數:
Get-ChildItem -Include *.txt -Recurse
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.