- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy items from one location to another location using PowerShell?
To copy items in PowerShell, one needs to use the Copy-Item cmdlet. When you use the Copy-Item, you need to provide the source file name and the destination file or folder name.
In the below example, we will copy a single file from the D:\Temp to the D:\Temp1 location.
Example
Copy-Item -Path D:\Temp\PowerShellcommands.csv -Destination D:\Temp1\ -PassThru
Output
PS C:\Windows\System32> Copy-Item -Path D:\Temp\PowerShellcommands.csv -Destination D:\Temp1\ -PassThru Directory: D:\Temp1 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 20-01-2020 12:10 1148809 PowerShellcommands.csv
In the above example, PowerShellCommands.csv file will be copied from the D:\Temp to the D:\Temp1 location. If the file already exists then it simply overwrites the file without any prompt or error or warning.
When you use the –Passthru parameter in the command, it displays the output in the console.
You can also rename items when you use the copy command. For that, you need to mention a new file name to the destination parameter.
Example
Copy-Item -Path D:\Temp\PowerShellcommands.csv -Destination D:\Temp1\PowerShel lcommands1.csv -PassThru
Output
PS C:\Windows\System32> Copy-Item -Path D:\Temp\PowerShellcommands.csv -Destination D:\Temp1\PowerShellcommands1.csv -PassThru Directory: D:\Temp1 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 20-01-2020 12:10 1148809 PowerShellcommands1.csv
While copying item(s) to another location, their attributes are also copies with them.
When you copy files from the source folder to the destination folder, and if the destination folder doesn’t exist then files(s) won’t get copied and it will throw an exception of DirectoryNotFoundException.
Example
For example, we will copy the above mentioned PowerShellcommands1.csv file to the unknown destination folder D:\Temp2 as shown below.
Copy-Item -Path D:\Temp\PowerShellcommands.csv -Destination D:\Temp2\PowerShel lcommands.csv -PassThru
Output
PS C:\Windows\System32> Copy-Item -Path D:\Temp\PowerShellcommands.csv -Destination D:\Temp2\PowerShellcommands.csv -PassThru Copy-Item : Could not find a part of the path 'D:\Temp2\PowerShellcommands.csv'. At line:1 char:1 + Copy-Item -Path D:\Temp\PowerShellcommands.csv -Destination D:\Temp2\ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand
- Related Articles
- How to copy files/folders to the remote location in the PowerShell?
- How to use Push-Location and Pop-Location command in PowerShell?
- How to use the Set-Location command in PowerShell?
- How to copy files from one folder to another using Python?
- How to copy files from one server to another using Python?
- How to test the shared location path from the remote computer in PowerShell?
- How to Copy Current Cell Address to Other Location in Excel?
- How to get the Azure Image publishers available at the location using PowerShell?
- How to use Location API in Android to track your current location using Kotlin?
- How to copy certain files from one folder to another using Python?
- How to get the location of the Azure VM using Azure CLI in PowerShell?
- 8085 program to move blocks of bits from source location to a destination location
- How to use Location API in Android to track your current location?
- How to copy Docker images from one host to another without using a repository?
- How to set location and location.href using JavaScript?
