I’m writing a simple backup script in Powershell, my first powershell script actually, so I could be missing something obvious.
In command line I run this:
PS C:\tmp> $source="C:\tmp\source" PS C:\tmp> $dest="C:\tmp\dest" PS C:\tmp> cp "$source\*.txt" "$dest\"
and all works fine, behaves as expected: copeies all .txt files from source to dest.
when I’m in script.ps1 file, have
copyBackupFiles ("C:\tmp\source", "C:\tmp\dest")
function copyBackupFiles($Source, $Dest)
{
Copy-Item "$Source\name.*" -Destination "$Dest\"
}
I get an error
Copy-Item : Cannot find path ‘C:\tmp\source C:\tmp\dest’ because it does not exist.
Any pointers on this problem? already spent a good couple of hours online with no luck…
Change the line from
to
It’s becase you pass to parameter
$sourcearray with 2 items, but no value to $dest.Unfortunatelly this is one of potential traps for people that know languages like C#, Java etc. Arguments are not delimitted by commas, but by space, much like in e.g. F#.
You can see wha happens with Trace-Command: