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
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