powershell - Get-ChildItem和Copy-Item说明

标签 powershell get-childitem copy-item

为什么

gci $from -Recurse | copy-item -Destination  $to -Recurse -Force -Container

行为与
copy-item $from $to -Recurse -Force



我认为应该是一样的,但是不一样。为什么?

最佳答案

您不会遍历文件/文件夹集合中的每个项目,而是将最后一个值传递给管道。您需要在复制项目命令中使用Foreach-item或%。从那里开始,您也不需要第二个-recurse开关,因为您已经在GCI中拥有了每一项。

试试这个:

gci $from -Recurse | % {copy-item -Path $_ -Destination  $to -Force -Container }

关于powershell - Get-ChildItem和Copy-Item说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21750311/

相关文章:

powershell - 无需等待即可复制项目

powershell - 将单个文件复制到多个目录[如果有则覆盖]

powershell - Powershell:将Foreach用于Get-EC2Snapshot

PowerShell "get-childitem2"大小超过 ~3.5gb

java - getChildren() add() 和 add()

powershell - 复制项目不包括 PowerShell 中源目录的子文件夹文件

windows - 使用不同凭据在windows机器之间复制文件的解决方案

windows-7 - 使用 PowerShell 运行应用程序并等待其完成

powershell - 如何使用Get-ChildItem在具有特定名称的目录中查找文件?

powershell - Powershell复制项目退出代码1