copy-item cannot bind argument to parameter 'path' because it is null

dave_mystic picture dave_mystic · Oct 5, 2013 · Viewed 10.8k times · Source

This one has me stumped. I have already searched on "copy-item cannot bind argument to parameter 'path' because it is null", and found several posts where the answer involved fixing the syntax so that the first parameter is not null. To the best of my knowledge, my first parameter is not null, and is also of the correct type, and I am still getting the same error. I also have a very similar script running on another server without problems, and which was the template I developed this from.

The script is in a file called 'weekly-archive.ps1' and is being invoked in the PowerShell console by '.\weekly-archive.ps1'.

I am running PowerShell 2.0 on Windows Server 2008 R2

Here is the script segment in question, complete with extra stuff to print the variables before use:

$pathtosource = "\\domainname\backup\servers\windowsimagebackup\"
$pathtodest = "G:\backup\servers\"
$images = Get-ChildItem $pathtodest
foreach ($_ in $images)
{
    $sourcepathname = $pathtosource + $_
    $destpathname = $pathtodest + $_
    $logresult += "`tSaving '" + $sourcepathname + "' to '" + $destpathname + "' at " + (Get-Date).ToShortTimeString() + ".`n"
    $sourcepathname
    $sourcepathname.GetType()
    $pathtodest
    $pathtodest.GetType()
    Copy-Item $sourchpathname $pathtodest -recurse
    $count += 1
}

And here is the resulting output for the first $_ in $images, showing that neither argument is null, and both arguments are actually strings:

PS D:\Administration> .\weekly-archive.ps1
\\domainname\backup\servers\windowsimagebackup\DC-1

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
G:\backup\servers\
True     True     String                                   System.Object
Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\Administration\weekly-archive.ps1:80 char:12
+         Copy-Item <<<<  $sourchpathname $pathtodest -recurse
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand

I also tried using the '-path' and '-destination' flags, which as AFAIK are optional. If I replace '$sourcepathname' in on the copy-item line in the script with a literal string, there is no error.

Finally, the following lines typed directly into the PowerShell Console work perfectly:

$sourcepathname = "\\domainname\backup\servers\windowsimagebackup\DC-1"
$pathtodest = "G:\backup\servers\"
copy-item $sourcepathname $pathtodest -recurse

So, something is clearly wrong with my use of '$sourcepathname', but I can't find it. Please do not hesitate to demonstrate my ignorance....

Answer

Keith Hill picture Keith Hill · Oct 5, 2013

There is a typo on the copy-item line of the script. You have $sourchpathname and it should be $sourcepathname.