Search code examples
powershellunccopy-item

Powershell copy-item won't copy txt files over unc path


Ok, so I am befuddled, I search high and low and have not be able to figure out how to get this to work.

Situation: I'm on workstation x and I need to copy *.txt from UNCpath1 to UNCpath2 recursively.

If I do this:

copy-item -path \\source\folders\* -destination \\destination\folders\$year\$month -recurse

everything works, but I get the folders as well.

These do not work:

copy-item -path \\source\folders\*.txt -destination \\destination\folders\$year\$month -recurse

copy-item -path \\source\folders\* -include "*.txt" -destination \\destination\folders\$year\$month -recurse

copy-item -path \\source\folders\* -filter "*.txt" -destination \\destination\folders\$year\$month -recurse

I have also tried variations on this and read high and low but can't figure it out. Scripting is obviously not my strength.

Thanks in advance.


Solution

  • Have you tried using Get-ChildItem and a foreach loop? Something like:

    $inputdir = 'UNC Path 1'
    $outputdir = 'UNC Path 2'
    $filetype = '*.txt'
    
    $files = Get-ChildItem -Path $inputdir -Filter $filetype -Recursive
    
    ForEach ($file in $files)
            { copy-item $file.Fullname -Destination $outputdir }     #using .Fullname to get the full pathname