如何在 PowerShell 中复制具有特定扩展名的文件?

powershellmicrosoft technologiessoftware & coding更新于 2026/2/15 15:37:17

要复制具有特定扩展名的文件,您需要使用 Get-ChildItem 命令。通过 Get-ChildItem,您首先需要检索具有特定扩展名的文件,然后需要使用管道 Copy-Item 命令。

在这里,我们需要将 *.txt 文件从源位置复制到目标位置。

首先,我们将检索源路径中所有可用的 *.txt 文件。

示例

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

输出

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

我们将把上述文件复制到目标文件夹。

示例

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

输出

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

相关文章