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:
(!$destination) {$destination
get
location}
(!(get
childitem
path $destination
exclude
*
.
)) {echo
"No child directories to copy to."
($overwritePrompt
eq
"y"
) {$overwritePrompt
'-Confirm'
{$overwritePrompt
''}
|
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.