9

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.

6 Answers 6

10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

where would I put that part into. I do want to keep the same file name in the multiple folders
Hey I am getting this error Join-Path : Cannot bind argument to parameter 'Path' because it is null. , any idea?
@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.
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 $_}
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.

5 Comments

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.CopyItemCommand
What is the value of $source?
the value of $source is \\ntsrv\common\Deployments\Boss\testing.txt
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 selected
I want to exclude some file while copy in the last destination, is there any idea how we can do?
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

Comments

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.

Comments

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

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.