Powershell Copy-Item Command Tutorial with Examples – POFTUT

Powershell Copy-Item Command Tutorial with Examples


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.

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.

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.

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

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.

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.

$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.

$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.

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  Linux cp Directory and Content

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.

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.

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.

PS> Copy-Item .\PerfLogs PerfBack -Recurse -Verbose
Verbose Copy Operation
Verbose Copy Operation

 

Leave a Comment