Solved

Copying file in Powershell

Posted on 2016-11-21
2
31 Views
Last Modified: 2016-12-21
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 adm.george[2]\Georgej
adm.george[2
]\AppData\LocalLow\Sun\Java\Deployment\security'
0
Comment
Question by:jgeorge3081
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 41896444
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

0
 

Author Closing Comment

by:jgeorge3081
ID: 41896701
Thanks alot Footech, worked great.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A brief introduction to what I consider to be the best editor for PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question