PowerShell 複製項目進度

PowerShell 複製項目進度

文件複製是涉及將一個或多個文件從來源轉移到目的地的常規活動。來源和目的地可以是相同的主機或兩個不同的主機。

Copy-Item 是每個 PowerShell 使用者用來將文件從一個地方複製到另一個地方的基本 cmdlet 之一。Copy-Item 唯一的缺點是它不會將操作的進度顯示為進度條或百分比。

當您有大量文件需要處理時,Start-BitsTransfer cmdlet 會變得非常方便,因為它顯示一個進度條,指示複製操作的進度。

本文將專注於 Start-BitsTransfer cmdlet,將文件從來源複製到目的地。

在 PowerShell 中使用 Start-BitsTransfer 複製文件

Start-BitsTransfer cmdlet 初始化一個 BITS 作業,無需麻煩即可將一個或多個文件從來源複製到目的地。通常,當 cmdlet 初始化 BITS 作業時,PowerShell 會變得非互動式。

此外,該 cmdlet 接受幾個可選參數以改善複製功能。以下是 Start-BitsTransfer cmdlet 的通用語法。

Start-BitsTransfer
[-Asynchronous]
[-Dynamic]
[-CustomHeadersWriteOnly]
[-Authentication <String>]
[-Credential <PSCredential>]
[-Description <String>]
[-HttpMethod <String>]
[[-Destination] <String[]>]
[-DisplayName <String>]
[-Priority <String>]
[-TransferPolicy <CostStates>]
[-ACLFlags <ACLFlagValue>]
[-SecurityFlags <SecurityFlagValue>]
[-UseStoredCredential <AuthenticationTargetValue>]
[-ProxyAuthentication <String>]
[-ProxyBypass <String[]>]
[-ProxyCredential <PSCredential>]
[-ProxyList <Uri[]>]
[-ProxyUsage <String>]
[-RetryInterval <Int32>]
[-RetryTimeout <Int32>]
[-MaxDownloadTime <Int32>]
[-Source] <String[]>
[-Suspended]
[-TransferType <String>]
[-CustomHeaders <String[]>]
[-NotifyFlags <NotifyFlagValue>]
[-NotifyCmdLine <String[]>]
[-CertStoreLocation <CertStoreLocationValue>]
[-CertStoreName <String>]
[-CertHash <Byte[]>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]

TransferType 參數用於指定複製操作的方向。它可以是下載或上傳。

讓我們在 D 磁碟中創建一個名為 codes 的文件夾,裡面有將近 300 個文本文件。此外,我們將創建另一個名為 copiedfiles 的空文件夾。

接下來,使用 Start-BitsTransfer 命令將文件從來源文件夾 codes 複製到目的地文件夾 copiedfiles。需要記住的一件事是在提前導入 BitsTransfer 模塊。

Import-Module BitsTransfer
Start-BitsTransfer -Source "D:\codes\*.txt" -Destination "D:\copiedfiles\" -TransferType Download

輸出:

Start-BitsTransfer

如上圖所示,當複製操作開始時,進度條會出現。這對於最終用戶來說是一個非常有用的指示器,可以了解操作的進度。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.