powershellrobocopycopy-item

Using copy-item in powershell to transfer file on a shared drive


I have an excel file that is used to perform certain operations on the database & that sheet needs to be copied right when the update is made. I have the script that does the update on the database but before that, I'm using the Copy-Item command to save the copy of the source excel file with a new name. But I'm getting a generic error like:

"Error/ getting/reading excel file \\shared\\documents\\myData.xlsx"  

Is that a permission issue or am I having an incorrect syntax. Or should I use robocopy command in powershell? Here's the chunk of powershell code that I have written so far:

#source file location
$fileLocation = "\\shared\documents\myData.xlsx"
#getting current date
$currentDate = Get-Date -Format "yyyy-MM-dd HH:mm"
#destination location
$backUpLocation = "\\shared\backup\myData_" + $currentDate + ".xlsx"
#copying file 
Copy-Item $fileLocation $backUpLocation

Any better solution is appreciated. Thanks in Advance.


Solution

  • Shoot! There was a silly mistake a file name cannot have ':' while the $currDate I was fetching is having a colon that's breaking. Copy-Item works best as it also allows renaming the file in the single command itself.