Solved

Copying file in Powershell

Posted on 2016-11-21
2
24 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
2 Comments
 
LVL 39

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now