GettingStarted
GettingStarted

Reputation: 7615

My custom function in PowerShell isn't working the way I expect it to

function CopyFiles($source, $destination)
{
    Write-Host "Source is " $source
    Write-Host "Destination is " $destination
    Copy-Item -Force -Recurse –Verbose $source -Destination $destination
}

When debugging, my source looks like it's combining the source and destination into a single string. is C:\shortcuts* \Server\Folder\User\person\Desktop

And destination looks empty

I am making a function call from my code that looks like

CopyFiles $source, $serverPath

Can anyone please explain what I am doing wrong or how I can fix it?

Upvotes: 0

Views: 28

Answers (1)

arco444
arco444

Reputation: 22841

You firstly have your string quoting wrong. It should be:

function CopyFiles($source, $destination)
{
    Write-Host "Source is $source"
    Write-Host "Destination is $destination"
    Copy-Item -Force -Recurse –Verbose $source -Destination $destination
}

Secondly, you're calling the function incorrectly. It should be

CopyFiles $source $serverPath

The way you're doing it, you actually pass the $source function argument as a string array. That's why you see them printed together and the destination as empty, you're not actually passing the $destination argument

Upvotes: 3

Related Questions