[英]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.