Search code examples
powershellcopy-item

Powershell Recursively writing to the same directory


I am trying to grab certain logs from a folder and copy them to my device. The code works, but the output is not what I expect (the other three log files are in the Logs folder). I would just like to have my three files appear. The output I get it below. The console complains about the directory already existing, but that is because it is created prior to my loop I think?

$filename = $env:COMPUTERNAME

$files = @('CAS.log', 'LocationServices.log', 'AppDiscovery.log', 'CCMExec.log')

foreach ($file in $files)
{
    Copy-Item "c:\Windows\CCM\Logs" -Filter $file -Destination 
    "\\REMOTECOMPUTERNAME\c$\RemoteLogStore\$filename" -Recurse
}

What shows up in RemoteLogStore


Solution

  • The $filename in "\\REMOTECOMPUTERNAME\c$\RemoteLogStore\$filename" is the same for every pass of foreach.

    Update $filename in the loop to what you actually want as a name, or just use the values from $files