How To Copy Files And Folders With PowerShell

PowerShell is a powerful tool that can be used to automate various tasks on your computer. One of the more popular PowerShell commands that we use Copy-Item, which allows for copying files and folders from one location to another. This article will show you how PowerShell can help you copy any file or folder with 3 simple steps!

1. PowerShell Core

PowerShell Core is PowerShell that runs cross-platform on Windows, Linux, and macOS. PowerShell Core shares many similarities with PowerShell in PowerShell in Windows PowerShell. To take advantage of the copy cmdlet on any platform, including Windows, make sure you use the latest PowerShell Core version (also known as PowerShell 7).

2. Copy Folders

In the below example, I have two folders (asp and code) to copy the asp folder to the code folder. I will use the following cmdlet. I use the -recurse to tell PowerShell to the content inside the asp as well.

 copy-item .\asp\ .\code\ -Recurse -Force

3. Copy files

Copying files is a bit easier since we don’t need to use the -recurse switch. The below example will copy a file from the asp folder to the code folder.

 Copy-Item .\asp\app1.sln .\code\

I can also rename the file at the destination path by using the following code.

Copy-Item .\asp\app1.sln .\code\app2.sln

Conclusion

Using PowerShell for copying files and folders can help you take advantage of PowerShell automation and save time either using a scheduled task or just to save time.


Posted

in

, ,

by