Saturday, October 17, 2020

Move Files from Source to Target using Powershell Scripts

Move-Item cmdlet

The Move-Item cmdlet moves an item (Objects like files), including its properties, contents, and child items, from one location to another location. The locations must be supported by the same provider.

Note: Move-Item cmdlet cannot override the file if the file already exists

Place the file inside the Source Folders



E.G. Moving a file from Source to Destination Folder



$SourcePath = "C:\iDaniel\Powershell\srcPath\Daniel.txt"
$DestinationPath = "C:\iDaniel\Powershell\trgtPath"
Move-item –path $SourcePath –destination $DestinationPath






check file got moved into target folders


E.G. Moving all the item which created on Current Day

get-childitem -Path "C:\iDaniel\Powershell\srcPath" -Recurse
Where-Object {$_.CreationTime -gt (Get-date).Date}
move-item -destination "C:\iDaniel\Powershell\trgtPath"






All the 10 files got move to target folders 




Note: by default, Get-Childitem does not move hidden files. To move hidden files, use the Force parameter with Get-Childitem Moving Hidden Files from Source to Target Folders




get-childitem -Path "C:\iDaniel\Powershell\srcPath" -Force -Recurse
Where-Object {$_.CreationTime -gt (Get-date).Date}
move-item -destination "C:\iDaniel\Powershell\trgtPath"







E.G. Moving Files based on File Type (Extension)
Create $Extension array which containing the list of Extension that need to move. After execution, all .txt files will move from source to destination folder

$Extension=@("*.*txt")
$SourcePath ="C:\iDaniel\Powershell\srcPath"
$DestinationPath = "C:\iDaniel\Powershell\trgtPath";
Get-ChildItem -recurse ($SourcePath) -include ($Extension)
move-Item -Destination ($DestinationPath)







Finally all the files with extension *.txt got moved into target Folders 


Thanks and Regards,
Daniel. $
Entrepreneur - Owner, East Canada INC,
Data Warehouse & Data Mining Architect

No comments: