Powershell Copy-Item to multiple computers

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

LVL 11
AcklesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alex GreenProject Systems EngineerCommented:
Copy-Item $source -Destination \\$computer\$destination -Recurse -Verbose -ErrorAction Stop

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

Try that
oBdACommented:
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

AcklesAuthor Commented:
Thanks.
It shows Green "Successfully Copied" however no content is there.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Alex GreenProject Systems EngineerCommented:
Do you have access rights to that folder, I.E you running it with an admin account.
AcklesAuthor Commented:
Yes I do, I can manually copy files in it
AcklesAuthor 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
oBdACommented:
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 SpecialistCommented:
Not perhaps copied but ends up as hidden?
AcklesAuthor 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
oBdACommented:
Including the additional modules that were supposed to be copied?
AcklesAuthor Commented:
The Hidden files are enabled, also not showing in command line
oBdACommented:
To verify: when you run the script as posted, the only output you see is the green success message?
AcklesAuthor Commented:
Yes
oBdACommented:
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).
AcklesAuthor 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
oBdACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AcklesAuthor 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
AcklesAuthor Commented:
& also the Green text, successfully copied
Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
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
AcklesAuthor Commented:
this is the source path:

"\\IT\Microsoft Powershell\Modules*"
AcklesAuthor 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
oBdACommented:
Do you have a network drive mapped to that location as well?
Are you running both ISE and console elevated?
AcklesAuthor 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....
AcklesAuthor 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?
oBdACommented:
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?
AcklesAuthor Commented:
Yes I can copy without any issues
oBdACommented:
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.
AcklesAuthor Commented:
I'll try tomorrow... about to leave....
AcklesAuthor 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)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.