Recently I was working on copying my work from one of my machine to another but wanted to excludes log files, report files and other text files.
I wanted all folder structure along with all my script files to remain intact, I achieved this by using below powershell code that I am sharing.
It will assist to those who want to achieve the same goal.
####################################################
$source = “c::\scripts”
$destination = “\\Hostname\c$\Scripts\”
$exclude = @(‘*.txt’,’*.log’,’*.csv’) ########exclude these file extensions from copying.
Get-ChildItem $source -Recurse -Exclude $exclude | Copy-Item -Destination {Join-Path $destination $_.FullName.Substring($source.length)} ##This will start copying & will preserve the folder structure.
###################################################
Join-Path is the key here that is helping in preserving the folder structure.
You can also use other logic to include only scripts files and maintain the folder hierarchy.
####################################################
$source = “c::\scripts”
$destination = “\\Hostname\c$\Scripts\”
$include = @(‘*.bat’,’*.ps1′,’*.py’) ########include these file extensions
Get-ChildItem $source -Recurse -Include $include | Copy-Item -Destination {Join-Path $destination $_.FullName.Substring($source.length)} ##This will start copying & will preserve the folder structure.
###################################################
You can use this small powershell code if you are in the same situation & want to just copy the files your desire.
Thanks for reading
Sukhija Vikas