How Can I Move a File or Folder from the Command Line Using Windows PowerShell?
If you would like to delete a file or folder using PowerShell, check out How Can I Delete a File or Folder from the Command Line Using Windows PowerShell? on Petri instead.
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.
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.
- 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.
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
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
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.
More in PowerShell
What is PowerShell and How to Get Started With It?
Sep 7, 2022 | Mike Kanakos
Use a PowerShell Substring to Search Inside a String
Aug 11, 2022 | Jeff Hicks
Test Network Connectivity with PowerShell Test-Connection
Aug 9, 2022 | Jeff Hicks
How to Use the PowerShell Exit Keyword to Terminate Scripts
Aug 8, 2022 | Ivan Mirchev
Error Handling With PowerShell Try Catch Blocks
Aug 5, 2022 | Adam Bertram
How to Use PowerShell Grep: Select-String and RegEx Real World Examples
Aug 2, 2022 | Jeff Hicks
Most popular on petri