如何将文件复制到PowerShell中的多个文件夹

bgr*_*rif 6 powershell powershell-ise powershell-3.0

我正在运行一个脚本,其中有多个环境,可以从弹出的窗口中选择.我遇到的唯一问题是当我想要设置脚本从我创建的源函数复制并立即将它放入多个位置时.

我需要帮助的部分代码发布在下面.

$Source = Select-Boss

$destination = 'D:\parts','D:\labor','D:\time','D:\money'

"Calling Copy-Item with parameters source: '$source', destination: '$destination'."

Copy-Item -Path $source -Destination $destination
Run Code Online (Sandbox Code Playgroud)

以下部分是如何在脚本中设置其余复制功能,以便您更好地了解主要部分副本的内容.

$Source = Select-Boss

$destination = 'D:\parts'

"Calling Copy-Item with parameters source: '$source', destination: '$destination'."

Copy-Item -Path $source -Destination $destination
Run Code Online (Sandbox Code Playgroud)

但是对于一个特定的部分,我需要将它复制到多个位置.我需要这样做,因为我不必更改我登录的服务器并转到另一个服务器.这一切都在一个地方完成,我希望让事情变得更容易,而不是写一大堆小编码去复制并保存在文件夹中.

Ric*_*ard 7

copy-item只为其-destination参数获取一个值,因此您需要某种类型的循环.

假设您想在多个文件夹中使用相同的文件名:

$destFolders | Foreach-Object { Copy-Item -Path $Source -dest (Join-Path $_ $destFileName) }
Run Code Online (Sandbox Code Playgroud)

应该这样做.