How to move files from one location to another location using PowerShell

In this article, Let me show you how to move files from one location to another location using PowerShell script. The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another location. The locations must be supported by the same provider.

Move-Item : Cannot create a file when that file already exists.

Example:1 Moving single files/folders

In the PowerShell window, type the command below and press ENTER. After the –path parameter, type the path of the file on your local PC that you want to copy, and after the –destination parameter, type the path of the destination folder. In the example below, I’m moving a file called URL_List.txt from the Source File Path folder to the Destination File Path folder.

$_SourcePath = "D:\BLOG\Power Shell\Examples\Source File Path\URL_List.txt"
$_DestinationPath = "D:\BLOG\Power Shell\Examples\Destination File Path"

Move-item –path $_SourcePath –destination $_DestinationPath

OR we can directly mention the path of files location without -path Keyword

Move-item $_SourcePath $_DestinationPath

Example:2 Moving all the item which created today.

From below code, we getting all files and folder which created today and move from “Source File Path” folder to “Destination File Path” folder.

get-childitem -Path "D:\BLOG\Power Shell\Examples\Source File Path" -Recurse |
Where-Object {$_.CreationTime -gt (Get-date).Date} |
move-item -destination "D:\BLOG\Power Shell\Examples\Destination File Path"

Also, by default, Get-ChildItem does not move hidden files. To move hidden files, use the Force parameter with Get-ChildItem.

Example:3 Moving the files based on File Type (extension)

I had created $_FileType array which contains the list of file types that need to move. After execution, all .html and .txt files will move from source to destination folder. Please refer to the snapshots to under stand the before and after the script execution

#location of starting directory
$_sourcePath ="C:\Users\dotnet-helpers\Desktop\SourcePath"
#location where files will be copied to 
$_destinationPath = "C:\Users\dotnet-helpers\Desktop\DestinationPath";
#Array of extension that need to move from source path
$_FileType= @("*html*", "*.*txt")

Get-ChildItem -recurse ($_sourcePath) -include ($_FileType) | move-Item -Destination ($_destinationPath)

OUTPUT:

Before Execution: Source Folder/Destination Folder

After Execution: Source Folder/Destination Folder

 

What do you think?

I hope you have an idea of how to move the files from one location to another location using the Powershell script. I would like to have feedback from the readers of my posts. Your valuable feedback, question, or comments about this article are always welcome.

 

7 thoughts on “How to move files from one location to another location using PowerShell”

  1. Hello
    I used your script to move files created today, but it moved files from today, yesterday, and day before. Did I miss something?

  2. G’day,

    Nice scripts.
    I specially liked this one

    Example:3 Moving the files based on File Type (extension)

    Only thing I noticed was a typo in the variable used as opposed to defined

    $_destinationPath

    move-Item -Destination ($destination)

    Should be move-Item -Destination ($_destinationPath)

    Other than that really cool scripts.

  3. Example 3 has typos in the destination variables..
    Other than that nice code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.