How to copy a file to multiple folders in PowerShell
Asked Answered
K

6

8

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.

Kinzer answered 10/7, 2014 at 14:57 Comment(0)
G
9

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.

Garwood answered 10/7, 2014 at 15:4 Comment(4)
where would I put that part into. I do want to keep the same file name in the multiple foldersKinzer
Hey I am getting this error Join-Path : Cannot bind argument to parameter 'Path' because it is null. , any idea?Malony
@DhartiSutariya Check you input – is the input collection empty? – but really this should be a new question where you can show a full re-create.Garwood
I used cp instead of Copy-Item to eliminate the need for Join-Path: "Q:\", "C:\Users\Robpol86\Downloads\", "A:\ISOs\Operating Systems\" | Foreach-Object {cp -v .\Win11_English_x64v1_NTLite.iso $_}Marinate
R
4

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.

Randellrandene answered 10/7, 2014 at 15:5 Comment(5)
I made the changes you suggested to do and it sitll doesn't work. what I get know is Copy-Item : The given path's format is not supported. At line:9 char:22 + Copy-Item -Path $source -Destination $dir + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommandKinzer
What is the value of $source?Randellrandene
the value of $source is \\ntsrv\common\Deployments\Boss\testing.txtKinzer
select-boss is a function the i created that opens a window that allows me to go to a folder and select a file in it and then it stores the results after it is selectedKinzer
I want to exclude some file while copy in the last destination, is there any idea how we can do?Malony
C
4

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
Cairngorm answered 9/5, 2018 at 18:28 Comment(0)
M
2

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.

Madalynmadam answered 9/11, 2014 at 5:13 Comment(0)
R
1
# 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
    }
}
Ricketts answered 9/9, 2016 at 19:58 Comment(0)
D
0

`$filesToCopy = get-Content C:\Users\lukhm\Desktop\Files.txt $destinationPath = Get-Content C:\Users\lukhm\Desktop\Folders.txt -force ForEach($folder in $destinationPath) { #Remove-Item -Path (Join-Path -Path (Join-Path -Path $PathTo -ChildPath $folder) -ChildPath $file_item) Copy-Item -Path $file_item -Destination $folder # Write-Verbose (Join-Path -Path $PathTo -ChildPath $folder) -verbose } }

Copy-Item : The filename, directory name, or volume label syntax is incorrect.
At line:18 char:9
+         Copy-Item -Path $file_item -Destination $folder
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Comman 
   ds.CopyItemCommand`

I have mentioned file path with file name, folder path with folder name in .txt files. How do I get the code to use them to copy mentioned files to mentioned folders?

Dement answered 25/11, 2022 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.