• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 740
  • Last Modified:

Need help with simply copy PowerShell script

I'm looking for quick help on putting a simple powershell script together that will copy files on Server A to Server B. It's a very simple setup; only one directory so no need to use recursion. I want to be able to use an input file and then something like a foreach or foreach-object to step thru each item on the text file and do the copy. Something like the code that I've attached. I realize that since my input file only contains file names and not the absolute path of the file, I need to construct that in or before the copy command. You're help is much appreciate it. I need help with this quick so I'm offering max points...I know it's simple but I'm not a Powershell guy.

$source = 'C:\Folder B'
$dest = 'C:\Folder A'
$data = get-content 'C:\input.txt'

Foreach ($i in $data)
{
Copy $source $dest
}

Open in new window

0
bndit
Asked:
bndit
  • 6
  • 4
1 Solution
 
Chris DentPowerShell DeveloperCommented:

Something like this should do it:

$source = 'C:\Folder B'
$dest = 'C:\Folder A'

Get-Content 'C:\input.txt' | %{
  Copy "$source\$_" $dest -WhatIf
}

That concatenates the $source path with a \ and the current entry from the file. I've added -WhatIf to allow you to check it's building the paths properly before committing to copying things around.

HTH

Chris
0
 
bnditAuthor Commented:
Thanks for the reply. However, it didnt work and I think it's because I changed my input file so that it contains the full paths for the files. The problem that I'm having is that when I run the copy command on the console it works..but the script it does not. For example, the command works when I run:

Copy-Item 'C:\Folder B\Copy README (44).txt' 'C:\Folder A'

My input file has full paths as follows:
'C:\Folder B\Copy README (44).txt'
'C:\Folder B\Copy README (43).txt'
'C:\Folder B\Copy README (42).txt'
'C:\Folder B\Copy README (41).txt'
'C:\Folder B\Copy README (40).txt'

So why is the Copy-Item $i $dest not working as expected?

$dest = 'C:\Folder A'
$data = Get-content 'C:\input.txt'
foreach ($i in $data) {
     Copy-Item $i $dest
     }

Open in new window

ps.JPG
0
 
Chris DentPowerShell DeveloperCommented:

It's the quotes (if those are in the file) get rid of those and retry?

Chris
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
bnditAuthor Commented:
The input file initially didnt have any quotes, and didn't work. So I figured I needed quotes; I've tried single and double quotes but no luck.

Again, when I try the command from the console, it works with single or double quotes, which I'd expect since there are spaces.
0
 
shatztalCommented:
I tried this and it work:
$dest = 'C:\Folder A'
$data = Get-content 'C:\input.txt'
foreach ($i in $data) {
     Copy-Item $i $dest
     }

the problem was that in the text file the input is not wright as in the error you se it says it cannot find the "c so the problem is in text you wrote
0
 
Chris DentPowerShell DeveloperCommented:

Yeah, you'll need the quotes if you're writing the command manually because the cmdlet will parse the line you feed it and read the space as a parameter delimiter.

That is, if you were to type:

Copy-Item C:\Folder B\etc

You'd need Quotes, or the first parameter would be "C:\Folder" and the second "B\etc".

But when you're reading in from a file you don't need quotes, the value is loaded into a string variable (in this case $i). The command is fed the parameter as a single string, even if it contains spaces.

With the quotes I can reproduce the error you get above about the drive not existing. Do you get exactly the same error when you remove the quotes completely from the text file?

Chris
0
 
bnditAuthor Commented:
Thanks Chris, I removed the quotes and it works now this time...I remember testing without quotes and it was not working...but then again I have different versions of the script as I was working out the bugs...anyway, it's working now. Thanks for the explanation as well. What resources do you recommend to get up to speed on powershell?
0
 
bnditAuthor Commented:
shatztal - I'm using PowerShell v1.0 on a Windows 2003 server. Did you by any chance try the script on a Windows Vista/7 machine with Powershell v2.0? Strangely enough, the script worked on my laptop with Windows 7...but not on my server until Chris pointed me to the quotes issue.
0
 
Chris DentPowerShell DeveloperCommented:

I can't give you any resources that I've used directly, I tend to wing it. But Brandon (who unfortunately doesn't post here any more) has quite a few in his profile here:

http://www.experts-exchange.com/M_4238767.html

Chris
0
 
bnditAuthor Commented:
Thanks Chris.
0
 
bnditAuthor Commented:
Thx
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now