How to Move a File to a Folder in PowerShell?

There are multiple ways you can move a file to a folder in PowerShell.

You can use Move-Item Cmdlet, Robocopy, etc., to move files in PowerShell to a folder.

Let me show you quickly, how to move a file to a folder in PowerShell using the Move-Item cmdlet.

To move a file to a folder in PowerShell, use the Move-Item cmdlet with the -Path parameter to specify the file’s current location and the -Destination parameter to define the target folder. For example, Move-Item -Path “C:\Source\file.txt” -Destination “C:\Destination” moves file.txt from C:\Source to C:\Destination.

Move a File to a Folder in PowerShell Using Move-Item Cmdlet

The primary cmdlet for moving files in PowerShell is Move-Item. This cmdlet can move files, rename items, and even use in registry keys. However, we’ll focus on its ability to move files from one location to another.

Basic Single File Move

Here’s a simple example of moving a file from one folder to another in PowerShell:

Move-Item -Path "C:\Source\file.txt" -Destination "C:\Destination"

In this script, Move-Item is the cmdlet used to initiate the move action. The -Path parameter specifies the current location of the file, and the -Destination parameter defines the folder where the file will be moved.

You can see the output in the screenshot below after I executed the PowerShell script.

Move a File to a Folder in PowerShell

Moving Multiple Files

To move multiple files in PowerShell using Move-Item, you can use wildcards (*) or specify multiple paths. Here is an example.

Move-Item -Path "C:\Source\*.txt" -Destination "C:\Destination"

This command will move all .txt files from the Source folder to the Destination folder.

Preserving Folder Structure

When you want to move files and maintain their directory structure, you can use the -Recurse parameter. This is particularly useful when moving a large number of files across nested folders.

Move-Item -Path "C:\Source\*" -Destination "C:\Destination" -Recurse

This command moves all items from the Source directory to the Destination directory, including subfolders and their contents in PowerShell.

Filtering Files While moving

There will be situations where you might want to move specific files before moving, using some filter criteria. Before moving them in PowerShell, you can filter files based on various attributes like extension, size, or last modified date.

Get-ChildItem -Path "C:\Source" -Filter "*.txt" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Move-Item -Destination "C:\Destination"

This script retrieves all .txt files from the Source folder that were last modified more than 30 days ago and moves them to the Destination folder.

Conditional Moves Based on File Content

Sometimes, you may need to move files based on their content. This requires reading the file and then deciding whether to move it.

Here is a complete PowerShell script.

Get-ChildItem -Path "C:\Source\*.log" | Where-Object { Get-Content $_.FullName | Select-String -Pattern "Error" } | Move-Item -Destination "C:\Destination"

This script moves .log files containing the word “Error” from the Source folder to the Destination folder.

Error Handling While Moving Files in PowerShell

When automating file moves, it’s important to include error handling to deal with issues like file locks or permissions. PowerShell provides the -ErrorAction parameter to handle such scenarios.

Move-Item -Path "C:\Source\file.txt" -Destination "C:\Destination" -ErrorAction SilentlyContinue

This script will attempt to move file.txt to the Destination folder and will silently continue if an error occurs, without throwing an exception.

Using Robocopy

For more complex file transfer needs, PowerShell can call external tools like Robocopy, a robust file copy command-line tool included with Windows.

robocopy "C:\Source" "C:\Destination" file.txt /MOV

This command uses Robocopy to move file.txt from the Source folder to the Destination folder. The /MOV parameter tells Robocopy to move the file instead of copying it.

Scheduling Automated Moves

PowerShell scripts can be scheduled to run at specific times using the Windows Task Scheduler. This is useful for recurring tasks like log file maintenance or backup routines.

$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-File "C:\Scripts\MoveFiles.ps1"'
$Trigger = New-ScheduledTaskTrigger -At 3am -Daily
$Task = Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "MoveFilesTask" -Description "Moves files daily at 3 AM"

This script creates a scheduled task that runs a PowerShell script (MoveFiles.ps1) every day at 3 AM.

Conclusion

PowerShell provides a powerful and flexible way to move a file to a folder. PowerShell Move-Item cmdlet is very useful for working with file movement.

In this PowerShell tutorial, I have explained, with examples, how to move files to a folder in PowerShell.

You may also like: