Copy, Move and Rename Files Using Windows PowerShell
Robocopy is still the tool I would recommend for performing bulk file management operations, but PowerShell also includes basic file management support. In this article, I’ll show you how to copy, move, delete and rename files using PowerShell.
Copying files using PowerShell
Start by opening Windows PowerShell in Windows 8.1. All the commands in this article are run in the context of the logged-in user.
- Press the WINDOWS key to switch to the Start screen.
- Type powershell and make sure that Windows PowerShell is selected in the search results on the left of the Start screen.
- Press ENTER to start Windows PowerShell.
In the PowerShell window, type the command below and press ENTER. After the –path parameter, type the path of the file on your local PC that you want to copy, and after the –destination parameter, type the path of the destination folder. In the example below, I’m moving a file called LicensedUsers.csv from the working directory to my desktop.
copy-item -path .LicensedUsers.csv -destination c:usersrusselldesktop
An error message will appear in the PowerShell window if the command doesn’t complete successfully. Using the copy-item cmdlet, you can also copy and rename a file at the same time by simply renaming the file in the path specified after the –destination parameter as shown here:
copy-item -path .LicensedUsers.csv -destination c:usersrusselldesktopLicensedUsers2.csv
You can copy the entire contents of a directory by omitting file names from both the –path and –destination parameters as shown below:
copy-item -path c:test -destination c:usersrusselldesktoptest –recurse
Filters can also be added to copy multiple files. For example, to copy all the .docx files from one folder to another, add the –filter parameter:
copy-item -path c:test –filter *.docx -destination c:usersrusselldesktoptest –recurse
Moving files using PowerShell
The move-item cmdlet follows exactly the same syntax as copy-item. In the examples below, I’ll show you a couple of additional parameters that can be used with both the move-item and copy-item cmdlets.
In the following example, I’ve added the –whatif parameter to check what the result of running the move-item cmdlet would be. The LicensedUsers.csv isn’t actually moved until you run the cmdlet for a second time and remove the –whatif parameter.
move-item -path .LicensedUsers.csv -destination c:usersrusselldesktop –whatif
For safety, you can require that the move or copy operation is confirmed by adding the –confirm parameter:
move-item -path .LicensedUsers.csv -destination c:usersrusselldesktop –confirm
Using the get-childitem cmdlet
To perform more complex copy and move operations, you can pipe the results of the get-childitem cmdlet to the move-item and copy-item cmdlets. The command line below moves all Word documents with the .docx file extension from the c:test folder to the desktop. When using the move-item cmdlet, the destination folder must exist otherwise an error will be thrown.
get-childitem -path c:test *.docx -recurse | move-item -destination c:usersrusselldesktop
Renaming and deleting files using PowerShell
The rename-item and remove-item cmdlets again follow a similar syntax. The command below renames the folder called test to test2.
rename-item –path c:test –newname c:test2
Now let’s delete all the items inside the test2 folder using the remove-item cmdlet:
And finally, we’ll delete the test2 folder.
With the examples I’ve shown you in this article, you can perform basic file operations from the command line using PowerShell and include the cmdlets in your PowerShell scripts.
More in PowerShell
How to Check Your PowerShell Version
May 4, 2022 | Michael Reinders
How to Download a File using PowerShell
Apr 21, 2022 | Michael Reinders
How to Write and Run a PowerShell Script File on Windows 11
Apr 4, 2022 | Ivan Mirchev
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Get-AdUser: How to Audit Active Directory Users with PowerShell
Mar 3, 2022 | Michael Reinders
The Top 10 PowerShell Commands That You Should Know
Feb 17, 2022 | Michael Reinders
Most popular on petri