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 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.
在复制文件和文件夹时,我们可以对其进行过滤。 如果我们不想复制指定的文件名。
根据扩展名过滤文件 (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

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