Using Powershell to Copy a Single File to Multiple Folders (en-US)

Using Powershell to Copy a Single File to Multiple Folders (en-US)

The Copy-Item command does as its name suggests and copies objects from one path to another. It does not however provision for the ability to copy one object to multiple locations.

The Get-ChildItem command retrieves an array of objects within a supplied directory. It does not however afford the ability to put child objects into the array of child objects.

The ForEach-Object command loops on an array of objects performing the tasks contained within its brackets.

#The purpose of this script is to take a file and copy it to each of the subfolders it is run in
Function fanCopy-item
{
Param
(
[string]$source= 'C:\Users\Mr. Marshall\Documents\My Documents\Clients\S-Z\T\Thomas Marshall\Computing Professional\Cover Letter.docx',
[string]$destination,
[string]$overwritePrompt
)
Process
{
# check the supplied source parameter value:
if(test-path-path $source) {$renFile= split-path $source-leaf-resolve}else {echo "Invalid source path provided."}
# check the supplied desintation parameter value:
if(!$destination) {$destination =get-location}
if(!(get-childitem -path $destination-exclude*.*)) {echo"No child directories to copy to."}
if($overwritePrompt -eq"y") {$overwritePrompt='-Confirm'}else {$overwritePrompt =''}
get-childitem -path $destination-exclude*.* |
Foreach-object { powershell-command"copy-item '$($source)' -destination '$($destination)\$($_.name)\$($_.name)-$($renFile)' $($overwritePrompt)"}
#An interesting note is that echos the location and I should've noticed the cover letter being copied to each of the subfolders in my user.
}
}

NOTE: This is a stub article. Please add more information as it becomes available.

Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Thomas W Marshall edited Revision 1. Comment: I wrote this function in a little over 4 hours. It is my first PowerShell function.

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Thanks.

  • Thomas W Marshall edited Revision 1. Comment: I wrote this function in a little over 4 hours. It is my first PowerShell function.

  • You are welcome.

Page 1 of 1 (3 items)