How to copy a file to multiple folders in PowerShell

浪子不回头ぞ 提交于 2019-11-28 04:04:26

问题


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.


回答1:


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) }

should do it.




回答2:


What I think you want is something like this:

$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
}

This code is making an array of folders, then iterates through each one, copying your file to it.




回答3:


https://www.petri.com/use-powershell-to-copy-files-to-multiple-locations

dir c:\work\*.txt | copy-item -Destination $destA -PassThru | copy -dest $destB -PassThru



回答4:


This is the best and easy solution which I have used.

In my case, it is a network location but it can be used for local system too.

"\\foo\foo" location contains 10 folders by user name. # use double slash (it's not showing here on StackOverflow)

dir "\\foo\foo\*" | foreach-object { copy-item -path d:\foo -destination $_ } -verbose

You must have write permission on the network share and destination folders.




回答5:


# 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
    }
}


来源:https://stackoverflow.com/questions/24679868/how-to-copy-a-file-to-multiple-folders-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!