具有示例的Powershell复制项命令教程

本文通过实例介绍Powershell Copy-Item cmdlet的基本用法,包括复制文件到指定目录、递归复制目录、跨服务器复制,以及覆盖、过滤和排除等高级功能。通过详细步骤,助你掌握这一强大的文件管理工具。
摘要由CSDN通过智能技术生成

Powershell provides Copy-Item cmdlet to copy an item from one location to another location. This item can be a file, folder or directory. Copy-Item cmdlet provides different features and options like overwrite, filter, exclude, verbose, progress etc. In this tutorial, we will learn these features by examples.

Powershell提供了Copy-Item cmdlet,可将项目从一个位置复制到另一位置。 此项可以是文件,文件夹或目录。 Copy-Item cmdlet提供了不同的功能和选项,例如覆盖,筛选,排除,详细,进度等。在本教程中,我们将通过示例学习这些功能。

将文件复制到指定目录 (Copy File To Specified Directory)

We will start with a simple example where we will copy given file to the specified directory. We will use just provide the source file and destination file names with their paths.

我们将从一个简单的示例开始,在该示例中,我们将给定文件复制到指定目录。 我们将只使用提供源文件名和目标文件名及其路径。

PS> Copy-Item .\config.sys test.sys

We can also use the -Destination option to specify the destination like below.

我们还可以使用-Destination选项指定目的地,如下所示。

PS> Copy-Item .\config.sys -Destination test.sys
Copy File To Specified Directory
Copy File To Specified Directory
将文件复制到指定目录

递归将目录复制到指定目录(Copy Directory To The Specified Directory Recursively)

We can also copy directory and its contents to the specified or destination directory by using -Recurse option. Recurse option will copy all sub files and folders of the given source directory to the destination directory.

我们还可以使用-Recurse选项将目录及其内容复制到指定目录或目标目录。 递归选项会将给定源目录的所有子文件和文件夹复制到目标目录。

PS> Copy-Item .\PerfLogs PerfBack -Recurse

将文件复制到远程服务器 (Copy File To The Remote Server)

Copy-Item is a very useful command where we can use copy local files and folders to the remote servers which support PS Remoting. In this example, we will specify the remote server or computer name with the -ComputerName option. We can use computer name hostname or IP address for this. In this example, we will copy to the remote server with the IP address 192.168.1.10

Copy-Item是一个非常有用的命令,我们可以在其中使用将本地文件和文件夹复制到支持PS Remoting的远程服务器上。 在此示例中,我们将使用-ComputerName选项指定远程服务器或计算机名称。 我们可以为此使用计算机名主机名或IP地址。 在此示例中,我们将复制到IP地址为192.168.1.10的远程服务器。

PS> Copy-Item .\PerfLogs PerfBack -ComputerName 192.168.1.10

将目录复制到远程服务器 (Copy Directory To The Remote Server)

We can also copy local folders and directories to the remote server or system. We will also use -ComputerName options like the previous example. We will also supply the -Recurse option in order to copy sub folders and directories.

我们还可以将本地文件夹和目录复制到远程服务器或系统。 我们还将像前面的示例一样使用-ComputerName选项。 我们还将提供-Recurse选项,以便复制子文件夹和目录。

PS> Copy-Item .\PerfLogs PerfBack -Recurse -ComputerName 192.168.1.10

将文件从远程服务器复制到本地服务器 (Copy File From Remote Server To The Local Server)

We can also copy the remote files from the remote server to the local system. We will create a session with the remote system with the New-PSSession cmdlet and use this session with the  -ToSession option. In this example, we will copy from server named DC1.

我们还可以将远程文件从远程服务器复制到本地系统。 我们将使用New-PSSession cmdlet与远程系统创建会话,并将此会话与-ToSession选项一起使用。 在此示例中,我们将从名为DC1服务器进行复制。

$Sessions=New-PSSession -ComputerName "DC1" -Credential "Poftut\ismail"

PS> Copy-Item "C:\test.txt" "D:\test.txt"  -ToSession $Sessions

将目录从远程服务器复制到本地服务器 (Copy Directory From Remote Server  To The Local Server)

We can also copy remote server directories recursively to the local system. We will use very similar command from the previous example. We will just provide the -Recurse option.

我们还可以将远程服务器目录递归复制到本地系统。 我们将使用与先前示例非常相似的命令。 我们将只提供-Recurse选项。

$Sessions=New-PSSession -ComputerName "DC1" -Credential "Poftut\ismail"

PS> Copy-Item "C:\PerfLogs" "D:\PerfLogs" -Recurse -ToSession $Sessions

覆盖文件 (Overwrite To The Files)

If there is same file or folder in the destination it will not be copied. If we want to copy even destination file or folder exist we need to force it with overwrite option. We will use -Force option in order to overwrite.

如果目标中有相同的文件或文件夹,则不会复制。 如果要复制甚至存在目标文件或文件夹,则需要使用覆盖选项强制它。 我们将使用-Force选项以进行覆盖。

PS> Copy-Item .\config.sys test.sys -Force

根据名称过滤文件 (Filter Files According To Name)

While copying files and folders we can filter them. If we do not want to copy specified file name.

在复制文件和文件夹时,我们可以对其进行过滤。 如果我们不想复制指定的文件名。

LEARN MORE  How To Copy and Move File with Python shutil Module?
了解更多如何使用Python shutil模块复制和移动文件?

根据扩展名过滤文件 (Filter Files According To Extension)

We can filter copied files according to their extensions. We will also use -Exclude or -Include options and provide the extension like .txt , .sql etc. In this example, we will filter and only copy txt files.

我们可以根据扩展名过滤复制的文件。 我们还将使用-Exclude-Include选项,并提供扩展名,如.txt.sql等。在此示例中,我们将过滤并仅复制txt文件。

PS> Copy-Item .\config.sys test.sys -Include "*.txt"

排除给定文件 (Exclude Given Files)

We can also exclude specified files with the -Exclude option. We will provide some part of the file name. In this example we will exclude file names starting with the Sql.

我们还可以使用-Exclude选项排除指定的文件。 我们将提供文件名的一部分。 在此示例中,我们将排除以Sql开头的文件名。

PS> Copy-Item .\config.sys test.sys -Exclude "Sql*"

详细复制操作 (Verbose Copy Operation)

During copy operation, we may need more information about the operation. We can print in verbose mode the copy operation with the -Verbose option.

在复制操作期间,我们可能需要有关该操作的更多信息。 我们可以使用-Verbose选项以详细模式打印复制操作。

PS> Copy-Item .\PerfLogs PerfBack -Recurse -Verbose
Verbose Copy Operation
Verbose Copy Operation
详细复制操作

翻译自: https://www.poftut.com/powershell-copy-item-command-tutorial-with-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值