Powershell Copy-Item to multiple computers

Ackles
Ackles used Ask the Experts™
on
Hello,
I need help in copying files from a network share to many computers....
I have the following script from internet, however I don't see the results...

        $computers=Get-Content C:\scripts\computer.txt

        $source = "IT\Microsoft Powershell\Modules\*"
        $destination = "C$\Windows\system32\WindowsPowerShell\v1.0\Modules\"

ForEach ($COMPUTER in ($computers))
        {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
        {write-host "Cannot reach $computer its offline" -f red}
          
else    {
        if ((Test-Path -Path \\$computer\$destination)){
        TRY{
        Copy-Item $source -Destination \\$computer\$destination -Recurse -Verbose -ErrorAction Stop
        Write-Host "Sucessfully copied on $computer" -BackgroundColor Green
        }
        Catch {$error[0].exception.message}
        }
        }
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexSenior Infrastructure Analyst

Commented:
Copy-Item $source -Destination \\$computer\$destination -Recurse -Verbose -ErrorAction Stop

\\$($computer)\$($destination) -Recurse -Verbose

Try that
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
What is it exactly you see?
Here's the reformatted script, so that you can actually determine what's happening from looking at it, and it fixes the issue that an inaccessible destination was silently ignored:
$computers = Get-Content C:\scripts\computer.txt
$source = "IT\Microsoft Powershell\Modules\*"
$destination = "C$\Windows\system32\WindowsPowerShell\v1.0\Modules\"

ForEach ($computer in ($computers)) {
	If (-not (Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)) {
		Write-Host "Cannot reach $($computer), it's offline" -ForegroundColor Red
	} Else {
		If (Test-Path -Path "\\$($computer)\$($destination)") {
			Try {
				Copy-Item -Path $source -Destination "\\$($computer)\$($destination)" -Recurse -Verbose -ErrorAction Stop
				Write-Host "Sucessfully copied on $($computer)" -BackgroundColor Green
			} Catch {
				Write-Host $_.Exception.Message -ForegroundColor Red
			}
		} Else {
			Write-Host "Target folder '\\$($computer)\$($destination)' not foubd or not accessible!" -ForegroundColor Red
		}
	}
}

Open in new window

Author

Commented:
Thanks.
It shows Green "Successfully Copied" however no content is there.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

AlexSenior Infrastructure Analyst

Commented:
Do you have access rights to that folder, I.E you running it with an admin account.

Author

Commented:
Yes I do, I can manually copy files in it

Author

Commented:
what I'm trying to do is,
A list of computer names
copy PowerShell modules from a network location to these computers directory, so that they can import modules
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Just to exclude the sometimes way too obvious stuff:
* have you verified that there's actually content in the source? You might want to define the source folder with a full absolute path, while you're at it.
* have you verified that the computer you're testing the results on is actually part of the computer list (and listed in the green message)?
What happens when you run, form the same PS console where you run the script from, run (obviously replacing '<computer>'):
gci -Recurse -Path '\\<computer>\C$\Windows\system32\WindowsPowerShell\v1.0\Modules'

Open in new window

Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Not perhaps copied but ends up as hidden?

Author

Commented:
* Yes, there is content
* Yes the computer is part, testing only with 1 computer
*When the command is run, I get list of all the modules & contents
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Including the additional modules that were supposed to be copied?

Author

Commented:
The Hidden files are enabled, also not showing in command line
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
To verify: when you run the script as posted, the only output you see is the green success message?

Author

Commented:
Yes
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
The -Verbose in the Copy-Item cmdlet will show each file that's copied.
That means that the source folder that the script sees is actually empty, or that the items in the source are all hidden (Copy-Item will by default skip hidden files).
Again: replace the source path with the full absolute path to the source folder (and make sure the files are not hidden).

Author

Commented:
Thanks,
The files are not Hidden.
The path I'm giving is \\....\... which is Absolute or do I miss something...
In the script I've pasted I have taken out the parts as don't want to write the names in public domain
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
That will show you all the files it will copy in a form right before copying. If you don't see the form pop up, you need to resolve the issues with the source path.
$computers = Get-Content C:\scripts\computer.txt
$source = "IT\Microsoft Powershell\Modules\*"
$destination = "C$\Windows\system32\WindowsPowerShell\v1.0\Modules\"

Get-ChildItem -Path $source -Recurse | Out-GridView
ForEach ($computer in ($computers)) {
	If (-not (Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)) {
		Write-Host "Cannot reach $($computer), it's offline" -ForegroundColor Red
	} Else {
		If (Test-Path -Path "\\$($computer)\$($destination)") {
			Try {
				Copy-Item -Path $source -Destination "\\$($computer)\$($destination)" -Recurse -Verbose -ErrorAction Stop
				Write-Host "Sucessfully copied on $($computer)" -BackgroundColor Green
			} Catch {
				Write-Host $_.Exception.Message -ForegroundColor Red
			}
		} Else {
			Write-Host "Target folder '\\$($computer)\$($destination)' not found or not accessible!" -ForegroundColor Red
		}
	}
}

Open in new window

Author

Commented:
Wow!
Now I get error:

et-ChildItem : An unexpected network error occurred.
At C:\Scripts\CopyModules.ps1:5 char:1
+ Get-ChildItem -Path $source -Recurse | Out-GridView
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (\\path\I...oft Powershell\:String) [Get-Chi
   ldItem], IOException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

Author

Commented:
& also the Green text, successfully copied
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
Well the source path is not in the right form... What are you using as source? and you should have permissions for ALL the subfolders inside that source path

Author

Commented:
this is the source path:

"\\IT\Microsoft Powershell\Modules*"

Author

Commented:
This is very strange, when I run the in the shell it works, but in ISE NOT...

can I stretch it a bit.... I mean the question
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Do you have a network drive mapped to that location as well?
Are you running both ISE and console elevated?

Author

Commented:
I'm running both elevated..
now when I run in shell it gives me error....
Get-ChildItem : An unexpected network error occurred.
At line:1 char:1
+ Get-ChildItem -Path $source -Recurse | Out-GridView

now it says, access to the path is denied....

Author

Commented:
I don't have network drive mapped to the location.
Do you suggest me to?

if yes, can we make it a part of the script?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
There should be no need for mapped drives; I was asking because Windows can behave strangely if you access the same network path both from an unelevated session (Explorer) and an elevated one (PS).
Are you able to copy without issues the folder manually using Explorer to the machine you're running the script on?

Author

Commented:
Yes I can copy without any issues
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Have you tried to run the script from another machine?
At this point, I'd restart the machine you're currently on. Since it sometimes work and sometimes not, I don't think this is related to PowerShell.

Author

Commented:
I'll try tomorrow... about to leave....

Author

Commented:
I think the issue is somehow with the share, so I made a share on the computer itself from where I want to run the script & that  works fine...
Thanks a lot oBdA (sorry, don't know your name)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial