Solved

Copying file in Powershell

Posted on 2016-11-21
2
15 Views
Last Modified: 2016-11-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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

14 Experts available now in Live!

Get 1:1 Help Now