Powershell script issue

powershell

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?

Best Answer

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

}