Yet another, arduous powershell question

$computers = Get-content 'C:\Powershell Projects\lastconnect\New Text Document.txt'
Foreach ($computer in $computers)
 {Copy-Item -Path '\\$Computer\Program Files (x86)\Quest Software\Toad for Oracle 10.5\ClientFiles\QSAuth11.key' -Destination 'c:\programs\powershell projects\$computer.txt'}

Open in new window


Afternoon,

So the script above, I know I can't put the variable in like that, but I can't find or figure out how to get it to work the way I need it to.

Could you help at all?

Cheers
Alex
LVL 16
Alex Green3rd Line Server SupportAsked:
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.

oBdACommented:
You can use variables like that, but you need double quotes around the string. A string in single quotes will be used verbatim by Powershell.
And though it's not required here since it's a plain vanilla variable (and not a property of an object), I prefer to put all variables appearing in strings consistently inside subexpressions:
$Computers = Get-content 'C:\Powershell Projects\lastconnect\New Text Document.txt'
ForEach ($Computer in $Computers) {
	Copy-Item -Path "\\$($Computer)\Program Files (x86)\Quest Software\Toad for Oracle 10.5\ClientFiles\QSAuth11.key" -Destination "C:\programs\powershell projects\$($Computer).txt"
}

Open in new window

1

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
Alex Green3rd Line Server SupportAuthor Commented:
Ahhhhh you're back!!!!

So it was the $ at the start and the double quotes..

I even put double quotes in to start but then thought I'd go with the single quotes.... Woe is me :(

Right,

I'm up to this now

$Computers = Get-content 'C:\Powershell Projects\audit\computers.txt'
ForEach ($Computer in $Computers){
    If (test-connection -computername $Computer -quiet) 
 {
	Copy-Item -Path "\\$($Computer)\c$\Program Files (x86)\Quest Software\Toad for Oracle 10.5\ClientFiles\QSAuth11.key" -Destination "C:\powershell projects\$($Computer).txt"
}

Else { 
        "$($computer) is not online" | Out-File -Append "C:\Powershell Projects\\computersnotonline.txt"
      }
}

Open in new window

0
Alex Green3rd Line Server SupportAuthor Commented:
Perfect
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Note: It would have been sufficient to use "\\$Computer\c$\...", but that only works if parsing allows for clear detection where the variable name ends. "\\${Computer}\c$\..." is more safe, but then it is not far from generally using "$($Computer)\c$\...", which technically uses a subexpression ( $(...) is a subexpression, allowing for expressions).
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.