如何在PowerShell中将文件复制到多个文件夹 [英] How to copy a file to multiple folders in PowerShell
问题描述
我正在运行一个脚本,其中包含多个环境,可以从弹出的窗口中进行选择.我遇到的唯一问题是当我想设置脚本以从我创建的源函数复制并一次将其放入多个位置时.
I am running a script that has multiple environments in it that can be selected from a window that pops up. the only problem I have run into is when I want to set up the script to copy from a source function that I have created and put it into multiple locations at once.
我需要帮助的代码部分贴在下面.
The part of the code I need help with is posted below.
$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
以下部分是如何在脚本中设置其余复制功能的部分,以便您更好地了解主要部分副本是什么.
The section below is how the rest of the copy functions are set up in the script, so you have a better understanding of what the main section copies are.
$Source = Select-Boss
$destination = 'D:\parts'
"Calling Copy-Item with parameters source: '$source', destination: '$destination'."
Copy-Item -Path $source -Destination $destination
但是对于一个特定的部分,我需要将其复制到多个位置.我需要这样做,因为我不必更改我登录的服务器并转到不同的服务器.这一切都在一个位置完成,我希望让事情变得更容易,而不是编写一大堆小代码来复制和保存在文件夹中.
But for one specific part I need to have it copied to multiple locations. I need this to happen since I don't have to change the server that I logged into and go to a different one. It is all done in one location and I was hoping to make things easier and not write a whole bunch of little coding to go and copy and save in the folders.
推荐答案
copy-item
的 -destination
参数只取一个值,所以你需要一个循环某种类型.
copy-item
only takes a single value for its -destination
parameter, so you need a loop of some type.
假设您想在多个文件夹中使用相同的文件名:
Assuming you want the same file name in multiple folders:
$destFolders | Foreach-Object { Copy-Item -Path $Source -dest (Join-Path $_ $destFileName) }
应该这样做.
这篇关于如何在PowerShell中将文件复制到多个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!