0

I am having an error with the script below:

$server_names = Get-Content "C:\ArchivedFolders.txt"
Foreach ($server in $server_names){
             Copy-Item "\\$server\C$\Mail\*" -Destination "\\10.100.101.50\mail\$server\" -Recurse
}

The purpose of the script is to copy a file from numerous folders and place them into numerous other folders. The error is below.

Copy-Item : Cannot retrieve the dynamic parameters for the cmdlet. Illegal characters in path.
At C:\mass_fetch.ps1:3 char:23
+              Copy-Item <<<<  "\\$server\C$\Mail\*" -Destination "\\10.100.101.50\mail\$server\" -Recurse
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.CopyItemCommand

Where did I go wrong?

4
  • It's hard to tell without actually look into what you pass as paths. Do below test: copy a line from file -> $server="pasteLineHere" -> dir "\\$server\c$\Mail". You might have blank space, invalid, or invisible characters in your server names. Also make sure your text file is in proper encoding.
    – strongline
    May 13, 2015 at 2:42
  • Why not remove the asterisk (*)? With -Recurse you're already copying everything.
    – Jeoh
    May 13, 2015 at 6:15
  • What does $server_names actually contain, after the Get-Content? Is it a collection of strings? Try feeding it to "get-Member" to find out. May 13, 2015 at 8:31
  • You might have success with Copy-Item -LiteralPath. You might also have success with this advice: stackoverflow.com/questions/2482771/…
    – Bacon Bits
    May 15, 2015 at 19:24

1 Answer 1

1

I think you need to escape the C$ in your source path. Your string is double-quoted, so PowerShell might think $\ is a variable.

$server_names = Get-Content "C:\ArchivedFolders.txt"
Foreach ($server in $server_names)
{
    Copy-Item "\\$server\C`$\Mail\*" -Destination "\\10.100.101.50\mail\$server\" -Recurse

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .