PowerShell basics: using Get-Content to drive a copy

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
C:\test\proc1.txt
C:\test\proc2.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

One Response to PowerShell basics: using Get-Content to drive a copy

  • DC says:

    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?

    Thanks.

Leave a Reply