Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 递归覆盖文件夹中的所有.txt文件_Powershell - Fatal编程技术网

Powershell 递归覆盖文件夹中的所有.txt文件

Powershell 递归覆盖文件夹中的所有.txt文件,powershell,Powershell,我可以在PowerShell中使用什么代码覆盖文件夹和子目录中所有.txt文件的内容?到目前为止,我尝试使用以下方法 获取所有文本文件,包括子目录中的文本文件,并将其存储在tvariable: $t = get-childitem *txt -recurse 然后使用设置contentcmdlet写入所有文件 set-content $t -value 'hello' 但这似乎不起作用。请帮忙 附言:我是个新手。不需要$t变量。可以使用管道: Get ChildItem-Filter*.tx

我可以在PowerShell中使用什么代码覆盖文件夹和子目录中所有.txt文件的内容?到目前为止,我尝试使用以下方法

获取所有文本文件,包括子目录中的文本文件,并将其存储在
t
variable:

$t = get-childitem *txt -recurse
然后使用
设置content
cmdlet写入所有文件

set-content $t -value 'hello'
但这似乎不起作用。请帮忙


附言:我是个新手。

不需要
$t
变量。可以使用管道:

Get ChildItem-Filter*.txt-Recurse | Set Content-Value'Hello!'
这将使用
Hello填充您所在目录中的所有文件以及所有子目录

如果出于某种原因需要进行处理并需要变量中的文件,则可以使用管道链或仅管道变量:

$t = get-childitem *txt -recurse
$t=Get ChildItem-Filter*.txt-Recurse
#用$t做东西
$t |设置内容-值“Hello!”
Microsoft文档:


您可以直接通过管道:
Get ChildItem-Filter*.txt-Recurse | Set Content
将自己的文件名设置为Content或应用一个值
Get ChildItem-Filter*.txt-Recurse | Set Content-value'Hello'
查看
Get Help Set Content-Parameter Path
-您会注意到您使用的参数是按位置设置的需要字符串,并且您正在提供fileinfo对象。[grin]尝试将集合管道化到
Set Content
或使用
ForEach Object
调用按参数名传入`$\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\。ForEach对象会更好。谢谢你的帮助。