Copy Function with attributes

I've been trying to write a simple function to copy a file and the set the attributes as needed:-
function tCopy($filSource, $filDestination) {
    Copy-Item $filSource, $filDestination

    $tmpSource = Get-Item $filSource
    $tmpDestination = Get-Item $filDestination

    $tmpDestination.CreationTime = $tmpSource.CreationTime
    $tmpDestination.LastWriteTime = $tmpSource.LastWriteTime
    $tmpDestination.LastAccessTime = $tmpSource.LastAccessTime
}

Open in new window


Which could be used by:-
tCopy C:\New\Application.txt, C:\New\cpApplication.txt

Open in new window


But I just get:-

Copy-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not
supported.
At line:2 char:15
+     Copy-Item $filSource, $filDestination
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.CopyItemCommand
 
Get-Item : Cannot find path 'C:\New\cpApplication.txt' because it does not exist.
At line:4 char:18
+     $tmpSource = Get-Item $filSource
+                  ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\New\cpApplication.txt:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
 
Get-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:5 char:32
+     $tmpDestination = Get-Item $filDestination
+                                ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemCommand
 
The property 'CreationTime' cannot be found on this object. Verify that the property exists and can be set.
At line:7 char:5
+     $tmpDestination.CreationTime = $tmpSource.CreationTime
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
The property 'LastWriteTime' cannot be found on this object. Verify that the property exists and can be set.
At line:8 char:5
+     $tmpDestination.LastWriteTime = $tmpSource.LastWriteTime
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
The property 'LastAccessTime' cannot be found on this object. Verify that the property exists and can be set.
At line:9 char:5
+     $tmpDestination.LastAccessTime = $tmpSource.LastAccessTime
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

Any idea what Im doing wrong?
tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You're calling it wrong.
You separated the arguments with a comma, so as far as PS is concerned, you're passing an array to the first positional argument.
tCopy C:\New\Application.txt C:\New\cpApplication.txt

Open in new window

But it's best to get used to using named arguments:
tCopy -filSource C:\New\Application.txt -filDestination C:\New\cpApplication.txt

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.