如何在 PowerShell 中複製具有特定副檔名的檔案?


要複製具有特定副檔名的檔案,你需要使用 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

更新日期:12-Mar-2020

5K+ 瀏覽

開啟職業生涯

完成此課程以獲得認證

開始
廣告