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.

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

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.

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

LEARN MORE  How To Copy Entire Directory In Linux?

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.

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.

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.

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.

LEARN MORE  Linux cp Directory and Content

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.

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 Copy Operation

Verbose Copy Operation

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)