Windows Server

How Can I Move a File or Folder from the Command Line Using Windows PowerShell?

In this article, I’ll show you how to use Windows PowerShell to move one or multiple files or folders from the command line, using the Move-Item, Get-Item, and Get-ChildItem PowerShell cmdlets.

Use PowerShell to move a file or folder

Let’s start by running a simple command to move a single file or folder. Make sure you are logged into the server or PC with an account that has full access to the objects you want to move.

  • Open a PowerShell prompt by clicking Start and type PowerShell. In the search results, click Windows PowerShell.
  • In the PowerShell console, type Move-Item –Path c:\testfolder -Destination c:\temp and press ENTER. Replace c:\testfolder with the full path to the folder you want to move; and c:\temp with the full path of the target folder.

The command above will move ‘testfolder’, and all its contents, to c:\temp. Additionally, the –Force parameter can be added to move hidden or read-only files.

  • To check the folder has been moved, type Get-Item -Path c:\* in the command prompt and press ENTER, replacing c: with the parent directory of the folder you just moved. The folder you moved shouldn’t be listed in the results. Adding * to -Path in Get-Item lists all child items in the directory.
Move file using PowerShell
Move file using PowerShell

More complex PowerShell move operations using Get-Item and Get-ChildItem

Before you can perform more complex move operations, you need to select the files that you want to move using either Get-Item or Get-ChildItem. I’ll explain the difference between the cmdlets below.

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?

  • In the PowerShell prompt, type Get-Item –Path c:\testfolder\*.txt | Move-Item -Destination c:\temp and press ENTER. This command moves all .txt files from the testfolder directory to c:\temp. But you could replace .txt with any file extension. For instance, .mp3 if you want to move MP3 files.
Move file using PowerShell - Move .txt files from command line using Get-Item and Move-Item PowerShell cmdlets
Move .txt files from command line using Get-Item and Move-Item PowerShell cmdlets

If you want to get all .txt files in testfolder and any subfolders, use Get-ChildItem with the -Recurse parameter instead of Get-Item. Get-Item doesn’t support the -Recurse parameter.

Get-ChildItem –Path c:\testfolder\*.txt -Recurse

And then pipe the results to Move-Item:

Get-ChildItem –Path c:\testfolder\*.txt -Recurse | Move-Item -Destination c:\temp

 

Work with one source and destination folder at a time

Because the above command line doesn’t recreate the directory structure in the destination folder, you cannot move two files with the same name. So, when using PowerShell to move files, it’s best to work with one source and destination folder at a time. Robocopy is a good tool for managing large and complex move operations.

Let’s make things simpler and go back to using Get-Item.

  • It’s possible to include a filter, like this:
Get-Item –Path c:\testfolder\* -Include *test*

Any file or folder in c:\testfolder, with the word test in its name will be displayed in the results.

  • The next command shows you what would happen if you tried to move all files and folders in testfolder that don’t include the word ‘test’.
Get-Item –Path c:\testfolder\* -Exclude *test* | Move-Item -Destination c:\temp -WhatIf
  • The –WhatIf parameter tests the command and lets you see the results, so you can be sure you won’t move anything you shouldn’t. To run the move operation, just remove the –WhatIf
Get-Item –Path c:\testfolder\* -Exclude *test* | Move-Item -Destination c:\temp

 

Move file using PowerShell - Use the –WhatIf parameter to test the PowerShell command and see the results without moving anything
Use the –WhatIf parameter to test the PowerShell command and see the results without moving anything.

With the basic operations that I’ve shown you in this article, using Move-Item, Get-Item, and Get-ChildItem, you should be able to perform most file/folder move tasks with PowerShell.

If you have more complex requirements, check out Robocopy. It’s a built-in command-line tool in Windows and Windows Server. And if you would like to delete files using PowerShell, check out How Can I Delete a Folder or File from the Command Line Using Windows PowerShell? on Petri.

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: