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

如何将文件复制到PowerShell中的多个文件夹,powershell,powershell-3.0,powershell-ise,Powershell,Powershell 3.0,Powershell Ise,我运行的脚本中有多个环境,可以从弹出的窗口中选择。我遇到的唯一问题是,我想将脚本设置为从我创建的源函数复制,并将其同时放入多个位置 下面是我需要帮助的代码部分 $Source=选择Boss $destination='D:\parts'、'D:\labor'、'D:\time'、'D:\money' 正在调用参数为source:'$source',destination:'$destination'的复制项 复制项目-路径$source-目标$Destination 下面的部分是如何在脚本中设

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

下面是我需要帮助的代码部分

$Source=选择Boss
$destination='D:\parts'、'D:\labor'、'D:\time'、'D:\money'
正在调用参数为source:'$source',destination:'$destination'的复制项
复制项目-路径$source-目标$Destination
下面的部分是如何在脚本中设置其余的复制函数,以便您更好地了解主要部分的复制是什么

$Source=选择Boss
$destination='D:\parts'
正在调用参数为source:'$source',destination:'$destination'的复制项
复制项目-路径$source-目标$Destination

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

copy item
只为其
-destination
参数取一个值,所以需要某种类型的循环

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

$destFolders | Foreach-Object { Copy-Item -Path $Source -dest (Join-Path $_ $destFileName) }

应该这样做。

我想你想要的是这样的:

$Source = Select-Boss

$destination = @("D:\parts","D:\labor","D:\time","D:\money")

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

foreach ($dir in $destination)
{
    Copy-Item -Path $source -Destination $dir
}

这段代码将创建一个文件夹数组,然后对每个文件夹进行迭代,将您的文件复制到其中。

这是我使用过的最好且简单的解决方案

在我的例子中,它是一个网络位置,但也可以用于本地系统

“\\foo\foo”
位置按用户名包含10个文件夹<代码>#使用双斜杠(这里没有在StackOverflow上显示)

您必须对网络共享和目标文件夹具有写入权限。

# Copy one or more files to another directory and subdirectories

$destinationFrom = "W:\_server_folder_files\basic"
$typeOfFiles = "php.ini", "index.html"
$filesToCopy = get-childitem -Path $destinationFrom -Name -include $typeOfFiles -Recurse
$PathTo = "W:\test\"
$destinationPath =  Get-ChildItem -path $PathTo -Name -Exclude "*.*" -recurse -force

foreach ($file_item in $filesToCopy)
{
    #Remove-Item -Path (Join-Path -Path $PathTo -ChildPath $file_item)
    Copy-Item -Path (Join-Path -Path $destinationFrom -ChildPath $file_item) -Destination $PathTo
    # Write-Verbose (Join-Path -Path $destinationFrom -ChildPath $file_item) -verbose

    ForEach($folder in $destinationPath)
    {
        #Remove-Item -Path (Join-Path -Path (Join-Path -Path $PathTo -ChildPath $folder) -ChildPath $file_item)
        #Copy-Item -Path (Join-Path -Path $destinationFrom -ChildPath $file_item) -Destination (Join-Path -Path $PathTo -ChildPath $folder)
        # Write-Verbose (Join-Path -Path $PathTo -ChildPath $folder) -verbose
    }
}

我做了你建议做的改变,但还是不起作用。我知道的是
复制项:不支持给定路径的格式。第9行字符:22+复制项目-路径$source-目的地$dir+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:未指定:(:)[Copy项目],不支持异常+完全合格者ID:,Microsoft.PowerShell.Commands.CopyItemCommand
$source的值是多少?$source的值是\\ntsrv\common\Deployments\Boss\testing.txt
select Boss
是我创建的一个函数,它打开一个窗口,允许我转到一个文件夹并选择其中的一个文件,然后在选择该文件夹后存储结果要在最后一个目的地复制时排除某些文件,您知道我们可以怎么做吗?我将把该部分放入何处。我确实希望在多个文件夹中保留相同的文件名。我遇到以下错误:连接路径:无法将参数绑定到参数“Path”,因为它为null,有什么想法吗?@DhartiSutariya检查您的输入-输入集合是否为空但实际上,这应该是一个新的问题,你可以展示一个完整的重新创建。
# Copy one or more files to another directory and subdirectories

$destinationFrom = "W:\_server_folder_files\basic"
$typeOfFiles = "php.ini", "index.html"
$filesToCopy = get-childitem -Path $destinationFrom -Name -include $typeOfFiles -Recurse
$PathTo = "W:\test\"
$destinationPath =  Get-ChildItem -path $PathTo -Name -Exclude "*.*" -recurse -force

foreach ($file_item in $filesToCopy)
{
    #Remove-Item -Path (Join-Path -Path $PathTo -ChildPath $file_item)
    Copy-Item -Path (Join-Path -Path $destinationFrom -ChildPath $file_item) -Destination $PathTo
    # Write-Verbose (Join-Path -Path $destinationFrom -ChildPath $file_item) -verbose

    ForEach($folder in $destinationPath)
    {
        #Remove-Item -Path (Join-Path -Path (Join-Path -Path $PathTo -ChildPath $folder) -ChildPath $file_item)
        #Copy-Item -Path (Join-Path -Path $destinationFrom -ChildPath $file_item) -Destination (Join-Path -Path $PathTo -ChildPath $folder)
        # Write-Verbose (Join-Path -Path $PathTo -ChildPath $folder) -verbose
    }
}
dir c:\work\*.txt | copy-item -Destination $destA -PassThru | copy -dest $destB -PassThru