View Single Post
Old 06-16-2008   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: Compare-Object and Get the name of object/File?

akcorr wrote:
Quote:

> I want to compare two directories and copy the files that are not in
> directory $A to directory $B. My initial try to see what I get back:
>
> $A = Get-ChildItem -path C:\test1
> $B = Get-ChildItem -path C:\test2
>
> $C = Compare-Object $A $B
Quote:

> How would I got about extracting the fullnames for each of those files?
> After that I can pass it to a function for work to be done.
I'd skip the foreach loop:

PS > $C|ForEach-Object{$_.InputObject}|ForEach-Object{$_.FullName}
C:\test2\bar.txt
C:\test1\foo.txt
PS >

That could also be more simply:
$C|ForEach-Object{($_.InputObject).FullName}

Hope that helps...

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec