PowerShell

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.

Sponsored Content

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?

  • 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 Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.
Live Webinar - Thursday, December 2nd! Active Directory Masterclass: AD Configuration Strategies for Stronger SecurityREGISTER NOW - Thursday, December 2, 2021 @ 1 pm ET

Active Directory (AD) is leveraged by over 90% of enterprises worldwide as the authentication and authorization hub of their IT infrastructure—but its inherent complexity leaves it prone to misconfigurations that can allow attackers to slip into your network and wreak havoc. 

Join this session with Microsoft MVP and MCT Sander Berkouwer, who will explore:

  • Whether you should upgrade your domain controllers to Windows Server
    2019 and beyond
  • Achieving mission impossible: updating DCs within 48 hours
  • How to disable legacy protocols and outdated compatibility options in
    Active Directory

Sponsored by: