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

输出:

开始位传输

如上图所示,当复制操作开始时进度条会出现。这是一个非常有用的指示器,能够让最终用户了解操作的进度。

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.