Copy, Move and Rename Files Using Windows PowerShell

In this easy Ask the Admin, I’ll show you how to perform basic file management operations using PowerShell in Windows Server and Windows 8.

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
Copying files using PowerShell in Windows Server. (Image Credit: Russell Smith)
Copying files using PowerShell in Windows Server. (Image Credit: Russell Smith)

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:

remove-item c:test2*.*

And finally, we’ll delete the test2 folder.

​remove-item c:test2

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.

Related Article: