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.
$_Name ---> $_.Name
Best regardsMartin [brima]
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,*.txtWill 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"}
Use $_.FullName instead of a $_.Name
$_ 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 *
$_. 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:
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.