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.
1 2 3 4 |
$_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
1 |
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.
1 2 3 |
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
1 2 3 4 5 6 7 8 |
#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.
Hello
I used your script to move files created today, but it moved files from today, yesterday, and day before. Did I miss something?
Above example scripts will helps to move the items with in servers/system.
Hi,
Will this work if my destination path is network location ?
Thanks Graham for highlighting.. Script corrected now in example.
Thanks Darsenator for highlighting :) .. Script corrected now in example.
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.
Example 3 has typos in the destination variables..
Other than that nice code.