close

Don't miss out on our next GET-IT conference about Virtual Desktop Infrastructure!

Home

PowerShell

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

Russell Smith

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

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.

The information in this article applies to Windows PowerShell, and PowerShell 7 and later versions on Windows 7, Windows 8.1, Windows 10, Windows 11, and all supported versions of Windows Server.

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.

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

Article saved!

Access saved content from your profile page. View Saved