Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

-inputobject vs. receiving from the pipe...

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-08-2007   #1 (permalink)
Hans Dingemans
Guest


 

-inputobject vs. receiving from the pipe...

The two examples of the where cmdlet below show different output, where I
expected these statements to execute the same. Why does the latter include
the sub directory?

PS> dir

Directory: Microsoft.PowerShell.Core\FileSystem::\test

Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 10/8/2007 5:11 PM sub
-a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
-a--- 10/3/2007 10:52 AM 27 sample.ps1

PS> $d = dir
PS>
PS> $d | where {$_.length -ge 0}

Directory: Microsoft.PowerShell.Core\FileSystem::\test

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
-a--- 10/3/2007 10:52 AM 27 sample.ps1

PS> where {$_.length -ge 0} -InputObject $d

Directory: Microsoft.PowerShell.Core\FileSystem::\test

Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 10/8/2007 5:11 PM sub
-a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
-a--- 10/3/2007 10:52 AM 27 sample.ps1

PS>



My System SpecsSystem Spec
Old 10-08-2007   #2 (permalink)
RichS
Guest


 

RE: -inputobject vs. receiving from the pipe...

The sub directory doesn't have a length. Could it be due to different
handling of the null length
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Hans Dingemans" wrote:
Quote:

> The two examples of the where cmdlet below show different output, where I
> expected these statements to execute the same. Why does the latter include
> the sub directory?
>
> PS> dir
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> d---- 10/8/2007 5:11 PM sub
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS> $d = dir
> PS>
> PS> $d | where {$_.length -ge 0}
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS> where {$_.length -ge 0} -InputObject $d
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> d---- 10/8/2007 5:11 PM sub
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS>
>
>
>
My System SpecsSystem Spec
Old 10-08-2007   #3 (permalink)
Bob Landau
Guest


 

RE: -inputobject vs. receiving from the pipe...

I've been working with Get-ChildItem lately, one of the things that I found
is that a directory does not have a Length property.

While I don't understand why there is a difference in these two statements
this may shed some light

$d | ? {$_.length -eq $null}
## for me returns _only_ the directories in $d



where {$_.length -eq $null} -inputobject $d
## for me returns nothing

Just why in the second case $length is a valid property i.e. can be compared
to 0 and the former is $null (can not be compared) will need to be answered
by someone smarter than I

thx
bob




"Hans Dingemans" wrote:
Quote:

> The two examples of the where cmdlet below show different output, where I
> expected these statements to execute the same. Why does the latter include
> the sub directory?
>
> PS> dir
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> d---- 10/8/2007 5:11 PM sub
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS> $d = dir
> PS>
> PS> $d | where {$_.length -ge 0}
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS> where {$_.length -ge 0} -InputObject $d
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> d---- 10/8/2007 5:11 PM sub
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS>
>
>
>
My System SpecsSystem Spec
Old 10-08-2007   #4 (permalink)
Jon
Guest


 

Re: -inputobject vs. receiving from the pipe...

It's because in one case the 'length' refers to the number of items in the
directory, and in the other it refers to the size of the individual
(FileInfo) elements


where {$_.length -ge 0} -InputObject $d

Here $d is an array, which has 3 elements ie its 'length' property is
greater than or equal to 0, so the object gets included. When you display
it you see all of its individual elements.


$d | where {$_.length -ge 3}

Here each element of the array gets passed along the pipeline and 'length'
is the length property of a fileinfo object

Compare these.....


where {$_.length -ge 3} -InputObject $d #Length of $d is 3 so the array
is included
where {$_.length -ge 4} -InputObject $d #Here it's not included, since
there's 3 items in the directory

--
Jon


"Hans Dingemans" <hans_dingemans@xxxxxx> wrote in message
news:Odbm17bCIHA.4712@xxxxxx
Quote:

> The two examples of the where cmdlet below show different output, where I
> expected these statements to execute the same. Why does the latter include
> the sub directory?
>
> PS> dir
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> d---- 10/8/2007 5:11 PM sub
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS> $d = dir
> PS>
> PS> $d | where {$_.length -ge 0}
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS> where {$_.length -ge 0} -InputObject $d
>
> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> d---- 10/8/2007 5:11 PM sub
> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>
> PS>
>
My System SpecsSystem Spec
Old 10-08-2007   #5 (permalink)
Keith Hill [MVP]
Guest


 

Re: -inputobject vs. receiving from the pipe...

"Jon" <Email_Address@xxxxxx> wrote in message
news:ehmn8dcCIHA.1208@xxxxxx
Quote:

> It's because in one case the 'length' refers to the number of items in the
> directory, and in the other it refers to the size of the individual
> (FileInfo) elements
>
>
> where {$_.length -ge 0} -InputObject $d
>
> Here $d is an array, which has 3 elements ie its 'length' property is
> greater than or equal to 0, so the object gets included. When you display
> it you see all of its individual elements.
>
>
> $d | where {$_.length -ge 3}
>
> Here each element of the array gets passed along the pipeline and 'length'
> is the length property of a fileinfo object
>
> Compare these.....
>
>
> where {$_.length -ge 3} -InputObject $d #Length of $d is 3 so the
> array is included
> where {$_.length -ge 4} -InputObject $d #Here it's not included,
> since there's 3 items in the directory
Nice. I would also add that the usage for the -InputObject parameter on
Where-Object is that the cmdlet evaluates the script block and outputs the
object passed to -InputObject depending on whether the scriptblock evaluated
to true or false. That is, the original object in its entirety is output
and not flattened. If you want to filter each element in the object and
pass only certain objects down the pipepline then use:

$d | where { $_.length -ge 0 }

as Jon points out.

--
Keith

My System SpecsSystem Spec
Old 10-08-2007   #6 (permalink)
Hans Dingemans
Guest


 

Re: -inputobject vs. receiving from the pipe...

Thanx to all.

Still very confusing that $_ variable has different context depending on
input via the pipe or input via the switch. It feels like we're missing
another variable here, say $__ for the containing collection, besides $_ for
the running object within the collection. V2.0?

Cheers,
Hans


"Jon" <Email_Address@xxxxxx> wrote in message
news:ehmn8dcCIHA.1208@xxxxxx
Quote:

> It's because in one case the 'length' refers to the number of items in the
> directory, and in the other it refers to the size of the individual
> (FileInfo) elements
>
>
> where {$_.length -ge 0} -InputObject $d
>
> Here $d is an array, which has 3 elements ie its 'length' property is
> greater than or equal to 0, so the object gets included. When you display
> it you see all of its individual elements.
>
>
> $d | where {$_.length -ge 3}
>
> Here each element of the array gets passed along the pipeline and 'length'
> is the length property of a fileinfo object
>
> Compare these.....
>
>
> where {$_.length -ge 3} -InputObject $d #Length of $d is 3 so the
> array is included
> where {$_.length -ge 4} -InputObject $d #Here it's not included,
> since there's 3 items in the directory
>
> --
> Jon
>
>
> "Hans Dingemans" <hans_dingemans@xxxxxx> wrote in message
> news:Odbm17bCIHA.4712@xxxxxx
Quote:

>> The two examples of the where cmdlet below show different output, where I
>> expected these statements to execute the same. Why does the latter
>> include the sub directory?
>>
>> PS> dir
>>
>> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>>
>> Mode LastWriteTime Length Name
>> ---- ------------- ------ ----
>> d---- 10/8/2007 5:11 PM sub
>> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
>> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>>
>> PS> $d = dir
>> PS>
>> PS> $d | where {$_.length -ge 0}
>>
>> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>>
>> Mode LastWriteTime Length Name
>> ---- ------------- ------ ----
>> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
>> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>>
>> PS> where {$_.length -ge 0} -InputObject $d
>>
>> Directory: Microsoft.PowerShell.Core\FileSystem::\test
>>
>> Mode LastWriteTime Length Name
>> ---- ------------- ------ ----
>> d---- 10/8/2007 5:11 PM sub
>> -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1
>> -a--- 10/3/2007 10:52 AM 27 sample.ps1
>>
>> PS>
>>
>

My System SpecsSystem Spec
Old 10-08-2007   #7 (permalink)
Steve Maillet \(MVP\)
Guest


 

Re: -inputobject vs. receiving from the pipe...

> Still very confusing that $_ variable has different context depending on
Quote:

> input via the pipe or input via the switch. It feels like we're missing
> another variable here, say $__ for the containing collection, besides $_
> for the running object within the collection. V2.0?
There already *IS* such a distinction. It's the -Inputobject switch itself
that forms the distinction. Using the | "moves" individual elements of a
collection down the pipeline and -InputObject moves an entire collection as
a single object into the current stage of the pipeline. (this would be the
primary use for the -InputObject switch.)

- Steve Maillet (MVP Windows Embedded CE)

My System SpecsSystem Spec
Old 10-08-2007   #8 (permalink)
Hans Dingemans
Guest


 

Re: -inputobject vs. receiving from the pipe...

Thanx Steve. I now see the exact purpose of it. Before, I was just thinking
there were two ways of doing the same thing, so why having two ways.

Point taken!!


"Steve Maillet (MVP)" <nospam@xxxxxx> wrote in message
news:%231A3zndCIHA.972@xxxxxx
Quote:
Quote:

>> Still very confusing that $_ variable has different context depending on
>> input via the pipe or input via the switch. It feels like we're missing
>> another variable here, say $__ for the containing collection, besides $_
>> for the running object within the collection. V2.0?
> There already *IS* such a distinction. It's the -Inputobject switch itself
> that forms the distinction. Using the | "moves" individual elements of a
> collection down the pipeline and -InputObject moves an entire collection
> as a single object into the current stage of the pipeline. (this would be
> the primary use for the -InputObject switch.)
>
> - Steve Maillet (MVP Windows Embedded CE)
>

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe a pipe command to a file WB VB Script 4 07-15-2008 09:03 PM
Add-Member -InputObject $s -MemberType ScriptMetho B.Williams PowerShell 10 04-07-2008 03:03 AM
getting arguments from pipe luntain PowerShell 6 11-23-2007 10:15 AM
ForEach-Object and -InputObject - Any use for it? Alex K. Angelopoulos [MVP] PowerShell 8 10-27-2006 12:41 AM
Get-Member -InputObject returns generic data; bug? Alex K. Angelopoulos [MVP] PowerShell 3 06-20-2006 07:40 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008