PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

Copy-Item : Cannot bind argument to parameter 'Path' because it is null.

rated by 0 users
Not Answered This post has 0 verified answers | 6 Replies | 3 Followers

Not Ranked
3 Posts
haxxess posted on 01-15-2010 1:54 AM

Im trying to understand a error that is being returned in powershell. Can somebody please help me understand where im going wrong with the following.

#### Error ###
Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:111
+ Get-ChildItem C:\users\noob\Desktop\source * -Recurse -Include *.docx,*.txt | ForEach-Object {Copy-Item <<<<
$_Name -Destination C:\users\noob\Desktop\destination}
+ CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand
#### End Error ###

.

#### Powershell ####
Get-ChildItem C:\users\noob\Desktop\source * -Recurse -Include *.docx,*.txt | ForEach-Object {Copy-Item $_Name -Destination C:\users\noob\Desktop\destination}
#### End Powershell ####

Im trying to copy all the *.docx & *.txt files scattered inside multiple directories from inside the source folder and place them all into the destination folder without maintaining the folder structure.

All Replies

Top 50 Contributor
98 Posts

$_Name  ---> $_.Name

Best regards
Martin [brima]

Not Ranked
3 Posts

Ah thanks, that fixed the bind argument error.

But now im facing another issue, the varables (if thats the correct word) are just the file names and not the full paths. If I run the following;
Get-ChildItem C:\users\noob\Desktop\source -Recurse -Include *.docx,*.txt
Will output the below ok;

    Directory: C:\users\noob\Desktop\source\folder1


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        15/01/2010   5:08 PM          0 aaa.docx
-a---        15/01/2010   5:06 PM         33 dfdfsdfs.txt


    Directory: C:\users\noob\Desktop\source


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        15/01/2010  12:18 PM          0 file.docx
-a---        15/01/2010   5:04 PM          0 New Text Document (2).txt
-a---        15/01/2010   5:04 PM          0 New Text Document (3).txt
-a---        15/01/2010   5:06 PM         16 New Text Document.txt

Then if I was to run the corrected Powershell line from outside a dirrerent working directory, I get "Cannot find path 'C:\aaa.docx' because it does not exist."
ect.. for each file..

PS C:\> Get-ChildItem "C:\users\sam.vaughan\Desktop\source" -Recurse -Include *.docx,*.txt | ForEach-Object {Copy-Item $_.Name -Destination "C:\users\sam.vaughan\Desktop\destination"}

Top 25 Contributor
572 Posts

Use $_.FullName instead of a $_.Name

Top 50 Contributor
98 Posts

$_ is a variable, correct a automatic variable.

You get more Infos about with:

Get-Help about_Automatic_Variables | more

Name and FullNames are Properties.

try this and you see all properties and methos for a file:

Get-ChildItem file.docx | Get-Member

and you can see the different FULLNAME and NAME with this command

Get-ChildItem file.docx | Format-List *

or shorter

gci file.docx | fl *

Top 10 Contributor
658 Posts

$_. is the current pipeline object used in script blocks, filters, the process clause of functions, where-object, foreach-object and switches. It is considered a "special pipeline variable". What comes after the $_. is the objects property.

To see what object properties there are for any given object use Get-Member like this:

  • Get-Service | Get-Member
  • Get-WmiObject Win32_Service | Get-Member

Not Ranked
3 Posts

I get it now :)
Thanks for your effort in the clarification, I can tell this place is going to be helping me out many times while im still learning PowerShell.

Again good effort.

Page 1 of 1 (7 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.