I want to create a script that copies files from a source directory on my local machine to a network drive that creates a folder path with the date in the name. I want to use a foreach loop to copy each file as a separate object. I'm sure there are some simpler ways to do this function but I am learning powershell and thought this would make for a good learning exercise for learning the ForEach-Object commandlet.
Here is what I have and the error I am getting, need help to figure out why? Thank You...
---------------- Script Starts Here--------------------
# Backup Script for Buddy's workstation
$dtoday = get-date -format "yyyyMMdd"
$sourcepath = "C:\Users\bfox\Desktop\Docs"
$destination = "\\file1\users$\bfox\backups\$dtoday"
$sourcefiles = get-childitem $sourcepath -file -Recurse
foreach ($file in $sourcefiles)
{
Copy-Item $sourcefiles -Destination $destination
}
------------------Script Ends here-------------------------
Here is the error I get:
Copy-Item : Cannot find path 'C:\Users\bfox\Desktop\Docs\WTX_8.1 Delpoys.docx' because it does not exist.
At C:\Users\bfox\Desktop\Docs\scripts\bud_backup.ps1:13 char:2
+ Copy-Item $sourcefiles -Destination $destination
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\bfox....1 Delpoys.docx:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand