How to copy files with the specific extension in PowerShell?


To copy the files with the specific extension, you need to use the Get-ChildItem command. Through the Get-ChildItem you first need to retrieve the files with the specific extension(s) and then you need to pipeline Copy-Item command.

Here, we need to copy *.txt files from the source to the destination location.

First, we will retrieve all the *.txt files available in the source path.

Example

Get-ChildItem D:\Temp\ -Filter *.txt

Output

PS C:\WINDOWS\system32> Get-ChildItem D:\Temp\ -Filter *.txt
    Directory: D:\Temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       26-01-2020     19:20          13818 aliases.txt
-a----       18-01-2020     18:25             48 delim.txt
-a----       18-01-2020     17:25             14 GetContentExample.txt
-a----       17-01-2020     22:04          55190 Processes.txt
-a----       18-01-2020     18:22          27620 ReadC.txt
-ar---       13-01-2020     18:19              0 Readonlyfile.txt
-a----       18-01-2020     18:44             22 stream1.txt
-a----       18-01-2020     16:26          27620 testreadC.txt

We are going to copy the above files to the destination folder.

Example

Get-ChildItem D:\Temp\ -Filter *.txt | Copy-Item -
Destination D:\TempContent\ -Force -PassThru

Output

PS C:\WINDOWS\system32> Get-ChildItem D:\Temp\ -Filter *.txt | Copy-Item -Destination D:\TempContent\ -Force -PassThru
    Directory: D:\TempContent
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       26-01-2020     19:20          13818 aliases.txt
-a----       18-01-2020     18:25             48 delim.txt
-a----       18-01-2020     17:25             14 GetContentExample.txt
-a----       17-01-2020     22:04          55190 Processes.txt
-a----       18-01-2020     18:22          27620 ReadC.txt
-ar---       13-01-2020     18:19              0 Readonlyfile.txt
-a----       18-01-2020     18:44             22 stream1.txt
-a----       18-01-2020     16:26          27620 testreadC.txt

Updated on: 12-Mar-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements