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

rated by 0 users
This post has 6 Replies | 3 Followers

Top 500 Contributor
Posts 3
haxxess Posted: 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.

Top 25 Contributor
Posts 92
Top Contributor

$_Name  ---> $_.Name

Best regards
Martin [brima]

Top 500 Contributor
Posts 3

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 10 Contributor
Posts 248
Microsoft MVP
Top Contributor

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

Top 25 Contributor
Posts 92
Top Contributor

$_ 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
Posts 634
Idera Employee

$_. 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

Top 500 Contributor
Posts 3

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
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.