locked
copy folder to many servers RRS feed

  • Question

  • hi everyone

    i need to copy folder ,

    his is what i tried :

    $source = "C:\N\mussab"

    $FileDestination = "C:\SBM\"

    $PC ="C:\n\servers2.txt"

    foreach ($computer in $PC) {Copy-Item $source "\\$computer\$destination" -Recurse}

    but this is not work , i receiving the below error :

    Copy-Item : The given path's format is not supported.

    please help me to fix this error , and if there's anther way to copy the folder that will be great


    Osma Othman

    Wednesday, December 12, 2018 7:07 PM

Answers

  • foreach ($computer in $PC) {Copy-Item $source "\\$computer\$filedestination" -Recurse}

    Likely would have to change $FileDestination to "C$\N\mussab" since it's a remote machine as well
    Wednesday, December 12, 2018 7:57 PM
  • $PC ="C:\n\servers2.txt" is pointing to a text file but you are using it as a collection in your loop, this won't work

    foreach ($computer in $PC) <--- $PC is not a collection. I think you meant to use Get-Content to get the content of the file line by line?

    Get-Content $PC | Foreach {
      Copy-Item ......
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, December 12, 2018 8:31 PM
  • Hi,

    Thanks for your question.

    You need to use "Get-Content" cmdlet to get the content of the text file.

    Also, you need to ensure you have write permissions to the destination path of remote computers.

    You also can use PSSession to solve it.

    $session = New-PSSession -ComputerName $computerName
    Copy-Item -Path $source -ToSession $session -Destination $FileDestination

    Please refer the link below:

    https://blogs.msdn.microsoft.com/luisdem/2016/08/31/powershell-how-to-copy-a-local-file-to-remote-machines/

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by Osama123 Thursday, December 13, 2018 6:30 AM
    Thursday, December 13, 2018 6:22 AM

All replies

  • foreach ($computer in $PC) {Copy-Item $source "\\$computer\$filedestination" -Recurse}

    Likely would have to change $FileDestination to "C$\N\mussab" since it's a remote machine as well
    Wednesday, December 12, 2018 7:57 PM
  • $PC ="C:\n\servers2.txt" is pointing to a text file but you are using it as a collection in your loop, this won't work

    foreach ($computer in $PC) <--- $PC is not a collection. I think you meant to use Get-Content to get the content of the file line by line?

    Get-Content $PC | Foreach {
      Copy-Item ......
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, December 12, 2018 8:31 PM
  • Hi,

    Thanks for your question.

    You need to use "Get-Content" cmdlet to get the content of the text file.

    Also, you need to ensure you have write permissions to the destination path of remote computers.

    You also can use PSSession to solve it.

    $session = New-PSSession -ComputerName $computerName
    Copy-Item -Path $source -ToSession $session -Destination $FileDestination

    Please refer the link below:

    https://blogs.msdn.microsoft.com/luisdem/2016/08/31/powershell-how-to-copy-a-local-file-to-remote-machines/

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by Osama123 Thursday, December 13, 2018 6:30 AM
    Thursday, December 13, 2018 6:22 AM
  • thank you all , its work now , i have ti use Get-content , thank you again

    Osma Othman

    Thursday, December 13, 2018 6:31 AM