header image

PowerShell basics: using Get-Content to drive a copy

Posted by: | June 24, 2011 | 1 Comment |

I recently saw a forum question about copying files where the file names where in a file

Icreated a file with the full path to the files that need to be copied

Get-ChildItem -path c:\test -Filter proc* | foreach {Add-Content -Value $_.Fullname -Path c:\test\files2copy.txt}

the content of the file can be seen

PS> Get-Content -Path c:\test\files2copy.txt

to copy the files to another folder

Get-Content -Path c:\test\files2copy.txt | foreach {Copy-Item -Path $_ -Destination c:\Teszzt2}


The trick with get-content is that it creates an array of strings – one per line of the file

Alternatively we could do this

$files = Get-Content -Path c:\test\files2copy.txt
foreach ($file in $files){Copy-Item -Path $file -Destination c:\Teszzt2}


the second option my be better if you need to perform further processing on the files

under: PowerShell Basics, PowerShellV2

1 Comment

  1. By: DC on December 13, 2012 at 10:34 am      

    Using this command:
    Copy-Item -Path \\”servername”\c$\Windows\System32\Winevt\Logs\Security.evtx
    -Destination \\”server”\d$\Logs\”servername”.evtx;

    How can I copy the security log from a server (that gets a value from a text file) to a central server and save the file name with the name of the current device being copied FROM?