Revision #1

You are currently reviewing an older revision of this page.
Go to current version

The Copy-Item command does as its name suggests and copies objects from one path to another.

The Get-ChildItem command retrieves an array of objects within a supplied directory.

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.
}
}
Revert to this revision