Windows PowerShell provides the Copy-Item
command or cmdlet in order to copy different types of items. Generally, the Copy-Item is used to copy files and folders easily. The Copy-Item can be also used to rename files by copying them. The -Destination
option is used to specify the destination path or name for the copied file.
Copy File
The most basic and popular usage of the Copy-Item command is copying a single file to the specified destination. The -Destination
option is used to specify the destination path or location.
PS> Copy-Item log.txt -Destination D:\Logs\
Copy Multiple Files Recursively
The Copy-Item can be also used to copy multiple files with a single command by using the recursive option. The -Recurse
option is used to enable recursive copy. In the following example, we copy all files and directories the C:\Logs\
into the D:\Logs
.
PS> Copy-Item C:\Logs\* -Destination D:\Logs\ -Recurse
Copy Directory with Contents Into New Directory
The Copy-Item can be also used to copy a directory and all of its contents into a new directory by using the -Recurse
option.
PS> Copy-Item C:\Logs\ -Destination D:\Logs\ -Recurse
Copy File By Renaming
We can use the Copy-Item by renaming the source file into a new name for the destination.
PS> Copy-Item C:\Logs\log.txt -Destination D:\Logs\newlog.txt
Copy File To A Remote Computer
The Copy-Item command can be used to copy files into a remote computer. In order to copy the remote computer first, a session should be accomplished into the remote computer by using the New-PSSession
command. Then the created session is provided to the Copy-Item command by using the -ToSession
option.
PS> $Session = New-PSSession -ComputerName 192.168.1.10 -Credential "ismail"
PS> Copy-Item "C:\Log\log.txt" -Destination "C:\Log\" -ToSession $Session
Copy Directory To A Remote Computer
The entire directory and all of its contents files and folders can be also copied to the remote computer. We will add the -Recurse
option to the previous command. In the following example, we copy the Log
folder to the remote system.
PS> $Session = New-PSSession -ComputerName 192.168.1.10 -Credential "ismail"
PS> Copy-Item "C:\Log\" -Destination "C:\Log\" -Recurse -ToSession $Session
Copy File From Remote Computer
The Copy-Item command can be used to copy files from a remote computer to the local computer. The New-PSSession
command is used to create a session with the remote computer. The -FromSession
is used to make a reverse connection and the source is assigned as a remote computer the destination is assigned as a local computer.
PS> $Session = New-PSSession -ComputerName 192.168.1.10 -Credential "ismail"
PS> Copy-Item "C:\Log\log.txt" -Destination "C:\Log\" -FromSession $Session
Copy Directory From Remote Computer
The Copy-Item can be also used to copy remote directories and their contents to the local computer. The -Recurse
option is used with the -FromSession
option.
PS> $Session = New-PSSession -ComputerName 192.168.1.10 -Credential "ismail"
PS> Copy-Item "C:\Log\" -Destination "C:\Log\" -Recurse -FromSession $Session