Simple ForEach Loop in Powershell

Having a brain fart right now -- I have a simple PowerShell script running a ForEach loop. Please see below:

# Define list of servers
$Servers = Get-Content C:\computersFile.txt

foreach ($Computer in $Servers) 
{ 
        
        # Create a backup of current file 
        Copy-Item "\\$remoteHost\$source" -Destination "\\$remoteHost\$destination"
       
}

Open in new window


$remoteHost is a placeholder just to show what I am trying to do .. I'd like to use the computer names in the computersFile.txt to specify my path like such "\\compuerName\C:\Program Files"

How could I dynamically populate that path ? If I use "$Computer" it attempts to put the entire txt file into that path.

I know I've done this before but for some reason cannot remember.

Any help is much appreciated.
LVL 1
SysAdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rastoiWindows DTS expertCommented:
in such loop every $Computer should contain one line from computersFile.txt
how is your source file formatted ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SysAdminAuthor Commented:
That's what I thought .. it is a simple .txt file with computer name 1 per line like this:

server01
server02
server03

Open in new window


Maybe I should use a .csv file instead?
1
rastoiWindows DTS expertCommented:
Ok. Just to valide run it such sample like you post here with write-host indtead of copy-item to see result
0
Shaun VermaakTechnical Specialist/DeveloperCommented:
Add this command to line 6 and post results. I expect you need to use $Computer.WhatEverIsInLine1
$Computer|Get-Member

Open in new window

0
SysAdminAuthor Commented:
Hey everyone thanks for the help -- here's what's really odd I just recreated the TXT file and placed it in a different directory and it's working as it should -- I thought I was crazy but I'm not!

Thanks for all your help. If I have any other issues I'll be sure to come back!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.