Copying file in Powershell

Guys I'm new to powershell and I'm trying to figure out why my code inst working here. I am trying to copy a file to a list of computers, but only to specific profiles. I've created 2 txt documents, one with a list of machines, another with a list of usernames. The code reads the computer name find, but it appends all the data from $users instead of the first entry.

function FileCopy ()
$computers = (Get-Content "C:\Pshell\computers.txt")
$fileToCopy = "\\Software\IT\sites.txt"
$users= (get-content C:\PShell\Users.txt)
$i= 0


ForEach($computer in $Computers){
    Copy-Item -Path $fileToCopy -Destination "\\$computer\c$\$Users[$i]\$Users[$i]\AppData\LocalLow\
    $i ++;

Error I get : Copy-Item : Could not find a part of the path '\\w10machine1\c$\Georgej[2]\Georgej[2
Who is Participating?
footechConnect With a Mentor Commented:
In your code above you would need to use a subexpression in the path string.  I'm guessing that the first "$Users[$i]" was just supposed to be "Users".  Example:
Copy-Item -Path $fileToCopy -Destination "\\$computer\c$\Users\$($Users[$i])\AppData\LocalLow\"

However, it seems like you want to copy the file to each computer in your list, and for each computer it should be copied to each of the user profiles in your list.  So you need to loop through both lists (nested loops).  No need to do any counting.
$Computers = (Get-Content "C:\Pshell\computers.txt")
$fileToCopy = "\\Software\IT\sites.txt"
$users= (Get-Content C:\PShell\Users.txt)

foreach ($computer in $Computers)
    foreach ($user in $users)
    { Copy-Item -Path $fileToCopy -Destination "\\$computer\c$\Users\$User\AppData\LocalLow\" }

Open in new window

jgeorge3081Author Commented:
Thanks alot Footech, worked great.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.