共用方式為


Copy-Item

將項目從某個位置複製到另一個位置。

語法

Path (Default) - FileSystem provider

Copy-Item
    [-Path] <String[]>
    [[-Destination] <String>]
    [-Container]
    [-Force]
    [-Filter <String>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-Recurse]
    [-PassThru]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-UseTransaction]
    [-FromSession <PSSession>]
    [-ToSession <PSSession>]
    [<CommonParameters>]

LiteralPath - FileSystem provider

Copy-Item
    [[-Destination] <String>]
    -LiteralPath <String[]>
    [-Container]
    [-Force]
    [-Filter <String>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-Recurse]
    [-PassThru]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-UseTransaction]
    [-FromSession <PSSession>]
    [-ToSession <PSSession>]
    [<CommonParameters>]

Path (Default) - All providers

Copy-Item
    [-Path] <String[]>
    [[-Destination] <String>]
    [-Container]
    [-Force]
    [-Filter <String>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-Recurse]
    [-PassThru]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-UseTransaction]
    [<CommonParameters>]

LiteralPath - All providers

Copy-Item
    [[-Destination] <String>]
    -LiteralPath <String[]>
    [-Container]
    [-Force]
    [-Filter <String>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-Recurse]
    [-PassThru]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-UseTransaction]
    [<CommonParameters>]

Description

Copy-Item Cmdlet 會將專案從一個位置複製到相同命名空間中的另一個位置。 例如,它可以將檔案複製到資料夾,但無法將檔案複製到憑證磁碟驅動器。

此 Cmdlet 不會剪下或刪除正在複製的專案。 Cmdlet 可以複製的特定專案取決於公開專案的 PowerShell 提供者。 例如,它可以複製檔系統磁碟驅動器中的檔案和目錄,以及登錄磁碟驅動器中的登錄機碼和專案。

此 Cmdlet 可以在相同的命令中複製和重新命名專案。 若要重新命名專案,請在 Destination 參數的值中輸入新名稱。 若要重新命名專案,而不複製該專案,請使用 Rename-Item Cmdlet。

範例

範例 1:將檔案複製到指定的目錄

本範例會將 mar1604.log.txt 檔案複製到 C:\Presentation 目錄。 不會刪除源檔。

Copy-Item "C:\Wabash\Logfiles\mar1604.log.txt" -Destination "C:\Presentation"

範例 2:將目錄內容複製到現有的目錄

本範例會將 C:\Logfiles 目錄的內容複製到現有的 C:\Drawings 目錄中。 不會複製 Logfiles 目錄。

如果 Logfiles 目錄有子目錄中的檔案,這些子目錄會以其檔案樹狀結構完整複製。 根據預設,容器 參數會設定為 True,這會保留目錄結構。

Copy-Item -Path "C:\Logfiles\*" -Destination "C:\Drawings" -Recurse

備註

如果路徑 C:\Drawings 不存在,Cmdlet 會將 Logfiles 資料夾樹狀目錄中的所有檔案複製到單一資料夾 C:\Drawings,並覆寫具有相同名稱的任何檔案。

範例 3:將目錄和內容複製到新目錄

本範例會複製 C:\Logfiles 來源目錄的內容,並建立新的目的地目錄。 新的目的地目錄,\Logs 會在 C:\Drawings中建立。

若要包含來源目錄的名稱,請複製到現有的目的地目錄,如範例 2 所示。 或者,將新的目的地目錄命名為與來源目錄相同的目錄。

Copy-Item -Path "C:\Logfiles" -Destination "C:\Drawings\Logs" -Recurse

備註

如果 Path 包含 \*,則所有目錄的檔案內容,包括子目錄樹狀結構,都會複製到新的目的地目錄。 例如:

Copy-Item -Path "C:\Logfiles\*" -Destination "C:\Drawings\Logs" -Recurse

範例 4:將檔案複製到指定的目錄,並重新命名檔案

此範例會使用 Copy-Item Cmdlet,將 Get-Widget.ps1 腳本從 \\Server01\Share 目錄複製到 \\Server12\ScriptArchive 目錄。 在複製作業中,命令會將專案名稱從 Get-Widget.ps1 變更為 Get-Widget.ps1.txt,以便安全地附加至電子郵件訊息。

Copy-Item "\\Server01\Share\Get-Widget.ps1" -Destination "\\Server12\ScriptArchive\Get-Widget.ps1.txt"

範例 5:將檔案複製到遠端電腦

會話會建立至名為 Server01 且具有 Contoso\User01 認證的遠端電腦,並將結果儲存在名為 $Session的變數中。

Copy-Item Cmdlet 會使用儲存在 test.log 變數中的工作階段資訊,將 D:\Folder001C:\Folder001_Copy 資料夾複製到遠端電腦上的 $Session 資料夾。 不會刪除源檔。

$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\User01"
Copy-Item "D:\Folder001\test.log" -Destination "C:\Folder001_Copy\" -ToSession $Session

範例 6:將資料夾複製到遠端電腦

會話會建立至名為 Server01 且具有 Contoso\User01 認證的遠端電腦,並將結果儲存在名為 $Session的變數中。

Copy-Item Cmdlet 會使用儲存在 D:\Folder002 變數中的會話資訊,將 C:\Folder002_Copy 資料夾複製到遠端電腦上的 $Session 目錄。 如果沒有使用 Recurse 參數,則不會複製任何子資料夾或檔案。 如果 Folder002_Copy 資料夾不存在,此作業就會建立該資料夾。

$Session = New-PSSession -ComputerName "Server02" -Credential "Contoso\User01"
Copy-Item "D:\Folder002\" -Destination "C:\Folder002_Copy\" -ToSession $Session

範例 7:以遞歸方式將資料夾的整個內容複製到遠端電腦

會話會建立至名為 Server01 且具有 Contoso\User01 認證的遠端電腦,並將結果儲存在名為 $Session的變數中。

Copy-Item Cmdlet 會使用儲存在 D:\Folder003 變數中的工作階段資訊,將整個內容從 C:\Folder003_Copy 資料夾複製到遠端電腦上的 $Session 目錄。 子資料夾會以其檔案樹狀結構完整複製。 如果 Folder003_Copy 資料夾不存在,此作業就會建立該資料夾。

$Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\User01"
Copy-Item "D:\Folder003\" -Destination "C:\Folder003_Copy\" -ToSession $Session -Recurse

範例 8:將檔案複製到遠端電腦,然後重新命名檔案

會話會建立至名為 Server01 且具有 Contoso\User01 認證的遠端電腦,並將結果儲存在名為 $Session的變數中。

Copy-Item Cmdlet 會使用儲存在 scriptingexample.ps1 變數中的工作階段資訊,將 D:\Folder004C:\Folder004_Copy 資料夾複製到遠端電腦上的 $Session 資料夾。 不會刪除源檔。

$Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\User01"
Copy-Item "D:\Folder004\scriptingexample.ps1" -Destination "C:\Folder004_Copy\scriptingexample_copy.ps1" -ToSession $Session

範例 9:將遠端檔案複製到本機電腦

會話會建立至名為 Server01 且具有 Contoso\User01 認證的遠端電腦,並將結果儲存在名為 $Session的變數中。

Copy-Item Cmdlet 會使用儲存在 test.log 變數中的會話資訊,從遠端 C:\MyRemoteData\D:\MyLocalData 複製到本機 $Session 資料夾。 不會刪除源檔。

$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\User01"
Copy-Item "C:\MyRemoteData\test.log" -Destination "D:\MyLocalData\" -FromSession $Session

範例 10:將遠端資料夾的整個內容複製到本機電腦

會話會建立至名為 Server01 且具有 Contoso\User01 認證的遠端電腦,並將結果儲存在名為 $Session的變數中。

Copy-Item Cmdlet 會使用儲存在 C:\MyRemoteData\scripts 變數中的會話資訊,將整個內容從遠端 D:\MyLocalData 資料夾複製到本機 $Session 資料夾。 如果 scripts 資料夾在子資料夾中有檔案,這些子資料夾會以其檔案樹狀結構完整複製。

$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\User01"
Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\" -FromSession $Session

範例 11:以遞歸方式將遠端資料夾的整個內容複製到本機計算機

會話會建立至名為 Server01 且具有 Contoso\User01 認證的遠端電腦,並將結果儲存在名為 $Session的變數中。

Copy-Item Cmdlet 會使用儲存在 C:\MyRemoteData\scripts 變數中的會話資訊,將整個內容從遠端 D:\MyLocalData\scripts 資料夾複製到本機 $Session 資料夾。 由於使用 Recurse 參數,因此作業會在腳本資料夾不存在時建立腳本資料夾。 如果 scripts 資料夾在子資料夾中有檔案,這些子資料夾會以其檔案樹狀結構完整複製。

$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\User01"
Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\scripts" -FromSession $Session -Recurse

範例 12:以遞歸方式將檔案從資料夾樹狀結構複製到目前資料夾

此範例示範如何將檔案從多層次資料夾結構複製到單一一平面資料夾。 前三個命令會顯示現有的資料夾結構和兩個檔案的內容,這兩個名稱 file3.txt

PS C:\temp\test> (Get-ChildItem C:\temp\tree -Recurse).FullName
C:\temp\tree\subfolder
C:\temp\tree\file1.txt
C:\temp\tree\file2.txt
C:\temp\tree\file3.txt
C:\temp\tree\subfolder\file3.txt
C:\temp\tree\subfolder\file4.txt
C:\temp\tree\subfolder\file5.txt

PS C:\temp\test> Get-Content C:\temp\tree\file3.txt
This is file3.txt in the root folder

PS C:\temp\test> Get-Content C:\temp\tree\subfolder\file3.txt
This is file3.txt in the subfolder

PS C:\temp\test> Copy-Item -Path C:\temp\tree -Filter *.txt -Recurse -Container:$false
PS C:\temp\test> (Get-ChildItem . -Recurse).FullName
C:\temp\test\subfolder
C:\temp\test\file1.txt
C:\temp\test\file2.txt
C:\temp\test\file3.txt
C:\temp\test\file4.txt
C:\temp\test\file5.txt

PS C:\temp\test> Get-Content .\file3.txt
This is file3.txt in the subfolder

Copy-Item Cmdlet 會將 Container 參數設定為 $false。 這會導致複製源資料夾的內容,但不會保留資料夾結構。 請注意,目的地資料夾中會覆寫同名的檔案。

範例 13:使用篩選來複製專案而不遞歸

此範例會使用 Include 參數來顯示結果,以選取要複製的專案。

此範例會使用包含要複製之檔案的下列資料夾結構:

  • D:\temp\tree\example.ps1
  • D:\temp\tree\example.txt
  • D:\temp\tree\examples\
  • D:\temp\tree\examples\example_1.txt
  • D:\temp\tree\examples\example_2.txt
  • D:\temp\tree\examples\subfolder\
  • D:\temp\tree\examples\subfolder\test.txt

在此範例中,會針對 Copy-ItemInclude 參數,使用通配符呼叫 。 為 Path 參數指定通配符可確保它會處理符合 D:\temp\tree\*的所有檔案和資料夾。 Include 參數會篩選要處理的項目清單,只將作業限製為開頭為 ex的路徑。

PS D:\temp\test\out> Copy-Item -Path D:\temp\tree\* -Include ex*
PS D:\temp\test\out> (Get-ChildItem -Recurse).FullName
D:\temp\out\examples
D:\temp\out\example.ps1
D:\temp\out\example.txt

Include 參數會套用至 D:\temp\tree 資料夾的內容,以複製符合 ex*的所有專案。 請注意,如果沒有遞歸,則會複製 D:\temp\out\examples 資料夾,但不會複製任何內容。

範例 14:使用篩選以遞歸複製專案

此範例會使用 Include 參數來顯示結果,以選取要複製的專案。

此範例會使用包含要複製之檔案的下列資料夾結構:

  • D:\temp\tree\example.ps1
  • D:\temp\tree\example.txt
  • D:\temp\tree\examples\
  • D:\temp\tree\examples\example_1.txt
  • D:\temp\tree\examples\example_2.txt
  • D:\temp\tree\examples\subfolder\
  • D:\temp\tree\examples\subfolder\test.txt

在此範例中,會針對 Copy-ItemInclude 參數,使用通配符呼叫 。 為 Path 參數指定通配符可確保它會處理符合 D:\temp\tree\*的所有檔案和資料夾。 Include 參數會篩選要處理的項目清單,只將作業限製為開頭為 ex的路徑。

D:\temp\out> Copy-Item -Path D:\temp\tree\* -Include ex* -Recurse
D:\temp\out> (Get-ChildItem -Recurse).FullName
D:\temp\out\examples
D:\temp\out\example.ps1
D:\temp\out\example.txt
D:\temp\out\examples\subfolder
D:\temp\out\examples\example_1.txt
D:\temp\out\examples\example_2.txt
D:\temp\out\examples\subfolder\test.txt

Include 參數會套用至 D:\temp\tree 資料夾的內容,以複製符合 ex*的所有專案。 請注意,使用遞歸,D:\temp\out\examples 資料夾會連同所有檔案和子資料夾一起複製。 此複本包含 不符合 include 篩選條件 的檔案。 使用 Copy-Item時,篩選只會套用至 Path 參數所指定的最上層。 然後遞歸會套用至那些相符的專案。

備註

Exclude 參數的行為與此範例中所述的行為相同,不同之處在於只會將作業限製為不符合模式的路徑。

範例 15:限制檔案從通配符指定的路徑遞歸複製

此範例示範如何限制從通配符比對路徑遞歸複製到另一個資料夾的檔案。 範例 13 顯示,由於 Include 參數只會篩選解析為通配符指定路徑 Path的路徑,因此 Include 參數無法用來限制從資料夾遞歸複製的檔案。 相反地,您可以使用 Get-ChildItem 來尋找您想要複製的專案,並將這些項目傳遞至 Copy-Item

此範例會使用包含要複製之檔案的下列資料夾結構:

  • D:\temp\tree\example.ps1
  • D:\temp\tree\example.txt
  • D:\temp\tree\examples\
  • D:\temp\tree\examples\example_1.txt
  • D:\temp\tree\examples\example_2.txt
  • D:\temp\tree\examples\subfolder\
  • D:\temp\tree\examples\subfolder\test.txt

若要複製以 ex*開頭的所有專案,請使用 Get-ChildItem 搭配 RecurseFilter 參數,並使用管線將結果傳送至 Copy-Item

D:\temp\out> Get-ChildItem -Path D:\temp\tree -Recurse -Filter ex* | Copy-Item
D:\temp\out> (Get-ChildItem -Recurse).FullName
D:\temp\out\examples
D:\temp\out\example_1.txt
D:\temp\out\example_2.txt
D:\temp\out\example.ps1
D:\temp\out\example.txt

不同於 Copy-ItemGet-ChildItem 參數會套用至遞歸期間探索到的專案。 這可讓您以遞歸方式尋找、篩選及複製專案。

參數

-Confirm

在執行 Cmdlet 之前,提示您進行確認。

參數屬性

類型:SwitchParameter
預設值:False
支援萬用字元:False
不要顯示:False
別名:cf

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Container

表示此 Cmdlet 會在複製作業期間保留容器物件。 根據預設,容器 參數會設定為 true

參數屬性

類型:SwitchParameter
預設值:True
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Credential

備註

任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要模擬其他使用者,或在執行此 Cmdlet 時提升您的認證,請使用 Invoke-Command

參數屬性

類型:PSCredential
預設值:Current user
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

-Destination

指定新位置的路徑。 預設值是目前的目錄。

若要重新命名複製的專案,請在 destination 參數的值中指定新的名稱。

參數屬性

類型:String
預設值:Current directory
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:1
必要:False
來自管線的值:False
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

-Exclude

指定一或多個路徑專案或模式,例如 "*.txt",以限制此 Cmdlet 的作業。 此參數的值會根據 Path 參數的通配符比對結果進行篩選,而不是最終結果。 只有在以一或多個通配符指定 Path 時,此參數才有效。 由於此參數只會篩選解析為 Path 參數的路徑,因此不會篩選使用 Recurse 參數遞歸子資料夾時探索到的任何專案。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:True
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Filter

指定篩選條件,以限定 Path 參數。 FileSystem 提供者是唯一已安裝且支援使用篩選的 PowerShell 提供者。 您可以在 about_Wildcards中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。

參數屬性

類型:String
預設值:None
支援萬用字元:True
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Force

表示此 Cmdlet 會複製無法變更的專案,例如透過只讀檔案或別名複製。

參數屬性

類型:SwitchParameter
預設值:False
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-FromSession

這是 FileSystem 提供者所提供的動態參數。

指定要從中複製遠端檔案 PSSession 物件。 當您使用此參數時,PathLiteralPath 參數會參考遠端電腦上的本機路徑。

如需詳細資訊,請參閱 about_FileSystem_Provider

參數屬性

類型:PSSession
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Include

指定一或多個路徑專案或模式,例如 "*.txt",以限制此 Cmdlet 的作業。 此參數的值會根據 Path 參數的通配符比對結果進行篩選,而不是最終結果。 只有在以一或多個通配符指定 Path 時,此參數才有效。 由於此參數只會篩選解析為 Path 參數的路徑,因此不會篩選使用 Recurse 參數遞歸子資料夾時探索到的任何專案。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:True
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-LiteralPath

指定通往一個或多個位置的路徑。 LiteralPath 的值會被原樣使用,不做任何更改。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。

如需詳細資訊,請參閱 about_Quoting_Rules

參數屬性

類型:

String[]

預設值:None
支援萬用字元:False
不要顯示:False
別名:PSPath

參數集

LiteralPath
Position:Named
必要:True
來自管線的值:False
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

-PassThru

傳回 物件,表示您正在使用的專案。 根據預設,此 Cmdlet 不會產生任何輸出。

參數屬性

類型:SwitchParameter
預設值:False
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Path

指定要複製之項目的路徑,做為字串陣列。 允許使用通配符字元。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:True
不要顯示:False

參數集

Path
Position:0
必要:True
來自管線的值:True
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

-Recurse

表示此 Cmdlet 會執行遞歸複製。

參數屬性

類型:SwitchParameter
預設值:False
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-ToSession

這是 FileSystem 提供者所提供的動態參數。

指定要複製遠端檔案的目標 PSSession 物件。 當您使用此參數時,Destination 參數會參考遠端電腦上的本機路徑。

如需詳細資訊,請參閱 about_FileSystem_Provider

參數屬性

類型:PSSession
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-UseTransaction

在作用中交易中包含 命令。 只有在交易進行中時,此參數才有效。 如需詳細資訊,請參閱 about_Transactions

參數屬性

類型:SwitchParameter
預設值:False
支援萬用字元:False
不要顯示:False
別名:usetx

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-WhatIf

顯示 Cmdlet 執行時會發生什麼事。 該 Cmdlet 未被執行。

參數屬性

類型:SwitchParameter
預設值:False
支援萬用字元:False
不要顯示:False
別名:無線

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

CommonParameters

此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters

輸入

String

您可以傳送包含路徑的字串到此 Cmdlet。

輸出

None

根據預設,此 Cmdlet 不會傳回任何輸出。

PSObject

當您使用 PassThru 參數時,這個 Cmdlet 會傳回代表複製項目的物件。

備註

Windows PowerShell 包含下列 Copy-Item的別名:

  • copy
  • cp
  • cpi

此 Cmdlet 的設計目的是要處理任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入 Get-PSProvider。 如需詳細資訊,請參閱 about_Providers