PowerShell: Move All files from One Folder to Another Recursively
Windows PowerShell allows users to perform all sorts of tasks by executing the appropriate commands. Using its Move-Item cmdlet, you can move files and folders on your PC, along with their contents. For someone new to PowerShell, it can be a little tricky to use PowerShell to move files from one folder to another.
And that’s what brings us here today. This article teaches you how to move files, folders, and subfolders in PowerShell. It discusses various user cases to help you grasp the practicality of the topic. Let’s dive in!
An Overview of Move-Item PowerShell Cmdlet
It’s fairly simple to move a file using the traditional cut-paste approach. But what if you have to perform the same task with dozens of files, folders, and subfolders? With PowerShell, you can easily move files and folders between various directories or to another folder without manually cut-and-paste each file.
The Move-Item command, with various parameters, is used to move files and folders with PowerShell. The basic syntax is:
Move-Item –Path c:testfolder -Destination c:myfolder
In this command, replace the c:testfolder with the original path of the file or folder you want to move and c:myfolder, with the path of the target folder. Use quotations if there’s a space in the folder name.
Some prominent parameters that you can add to the Move-Item cmdlet are:
Parameter | Key Notes |
---|---|
Move-Item | Move an item, folder, or subfolder to another location |
-Force | To overwrite an existing file with the same name in the target folder. |
-Verbose | To get detailed information about the operation done by the command. |
.txt filter | Used after the file/folder path to move only the .txt files. Replace .txt with .pdf to move pdf files. |
Get-Children with Recurse | Recursively move the specific files like .pdf from a directory and all its sub-directories. |
Basic Tips to Use PowerShell
- If you’re new to PowerShell, it’s best to use the command to move one file. Once you get better at it, add various parameters to the Move-Item cmdlet to test its complex versions.
- Always open PowerShell with Administrative permissions. To do so, search for PowerShell in the Starts menu. When its name pops up, right-click it and hit “Run as administrator.”
User Cases of Moving Files and Folders in PowerShell
This section outlines several examples of using the right PowerShell script to move files from one folder to another.
Example 1: Move a Single File to Another Location in PowerShell
To move a single file to another location using PowerShell, you need to mention its name, original path, and the target path. For instance, take a look at the command below:
Move-Item -Path "C:\SourceFolder\example.txt" -Destination "E:\DestinationFolder"
This cmdlet with move the example.txt file in the Source Folder in C directory to the Destination Folder in E directory.
If you also want to rename the file while moving it, use the command:
Move-Item -Path "C:\SourceFolder\example.txt" -Destination "E:\DestinationFolder/myfile.txt"
This command renames the file example.txt file to myfile.text when moving it. Change the file name and destination in this “move item PowerShell script”according to your requirements.
Example 2: Rename an Existing File With the Same Name
If the destination folder already contains a file named “example.txt” and you want to rename it, use the “-Force” parameter.
Move-Item -Path "C:\SourceFolder\example.txt" -Destination "E:\DestinationFolder" -Force
This shows that moving a file in PowerShell is very convenient.
Example 3: Moving Multiple Files Using PowerShell
With PowerShell, it’s possible to move multiple files in a folder to another location. To achieve this, the user needs to specify the names of each file, separated by a comma (,). Below is the basic PowerShell script to move files to another folder:
Move-Item -Path "C:\SourceFolder\example1.txt", "C:\Temp\example2.txt" -Destination "E:\DestinationFolder"
In this example, you successfully move two files named example1.txt and example2.txt from the “C:\SourceFolder” directory to the "E:\DestinationFolder” directory.
Example 4: PowerShell Move Entire Folder to Another Folder
If you wish to move an entire folder and its content, use the command:
Move-Item -Path "C:\Temp\SourceFolder\*" -Destination "E:\Archives"
This script moves files in the Source Folder directory in “C:\Temp” to the "E:\Archives" directory. Here, the character (*) specifies the source files.
Similarly, you can execute the below PowerShell script to move the C:\Temp directory, including its subdirectories and files, to E\Archives:
Move-Item -Path "C:\Temp\*" -Destination "E:\Archives"
Example 5: Move Files Based on Extension in PowerShell
One key advantage of using PowerShell is it empowers users to move specific files based on their names or extensions. For instance, if a folder contains thousands of files with different extensions and you wish to move only the .txt files, use the command:
Move-Item -Path "C:\SourceFolder\*.txt" -Destination "E:\DestinationFolder"
This command moves all .txt files from the source directory to the destination directory, leaving behind the remaining files. Similarly, you can use the below PowerShell script to move PDF files only:
Move-Item -Path "C:\SourceFolder\*.pdf" -Destination "E:\DestinationFolder"
Example 6: Recursively Move Files Based on Extension to Another Directory
If the .pdf or .txt files are present in the subdirectories, the above command will not detect them. In this scenario, you need to use the “Get-Children with the Recurse” parameter to recursively move the .pdf files in every subdirectory of the Source Folder.
To move files and folders to another folder recursively with PowerShell:
Get-ChildItem -Path "C:\SourceFolder" -Recurse -Filter *.pdf | Move-Item -Destination "E:\DestinationFolder"
This command will retrieve all the PDF files from the Source Folder and its subfolders and move them to the destination folder.
Here again, you can use the -Force parameter to ensure the existing files in the destination folder are overwritten. Moreover, use the -Verbose command to get more details about this operation.
Get-ChildItem -Path "C:\SourceFolder" -Recurse -Filter *.txt | Move-Item -Destination "E:\DestinationFolder" -Force -Verbose
Now, you have enough information to use PowerShell scripts to move files and folders to another folder on your Windows.
Bonus Tip: Manage PowerShell Commands and Administrative Tasks Remotely
AirDroid Remote Support is a versatile tool with a range of impressive features for granting secure access to remote Windows PCs from anywhere. The employers can connect the whole IT infrastructure and assign specific roles and tasks to each user.
On the personal front, the software lets you access unattended devices and perform administrative tasks like executing PowerShell commands conveniently. The admin has the authority to force a black screen on the controlled device to ensure 100% privacy.
Then, there can be collaborative functions, such as real-time screen sharing, voice chatting, messaging and file transfer, and permission management. All these functions make it worth having remote software for both personal and business use.
FAQs
Q1: Command Prompt vs PowerShell, which is better?
Both Command Prompt and PowerShell allow Windows users to handle various tasks. But consider PowerShell as an advanced version of Command Prompt that lets users execute scripts to quickly accomplish administrative tasks. It is more versatile and feature-rich than Command Prompt
Q2: What happens if the same filename already exists at the destination?
If there’s a file with the same name in the destination folder, PowerShell will give an error on executing the Move-Item cmdlet. However, you can use the -Force parameter at the end of the command to overwrite the destination file if needed.
Q3: Can I move a file and rename it at the same time with PowerShell?
Yes, you can move and rename the file with PowerShell simultaneously. For this, mention the new file name in the Destination path. For instance,
Move-Item -Path "C:\SourceFolder\example.txt" -Destination "E:\DestinationFolder/myfile.txt"
This will rename the example.txt file to myfile.txt when moving it to the destination.
Final Remarks
Mastering how to use PowerShell to move folders and subfolders will help you automate tedious tasks effortlessly. Now, you shouldn’t experience any technical issues in moving files and folders in PowerShell to another location.
Try moving a single file before you proceed to handle multiple files or complete folders. If you encounter any issues, reach out to us through the comment section.
Moreover, we introduced AirDroid Remote Support to access and manage your remote devices from anywhere. With this tool, you can:
- Access and control both attended and unattended devices.
- Assign tasks, with varying levels of permissions, to various users.
- Collaborate effectively with your team.
- Force a black screen on the remote device when performing a task.
Leave a Reply.