Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copying file in Powershell

Posted on 2016-11-21
2
Medium Priority
?
42 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 41

Accepted Solution

by:
footech earned 2000 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

598 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