0

Can I not use a WHERE in the pipe after an @{name alias?

I have to use custom column headers in my output so I am using the @{name syntax. Later in the pipe, the sort works using the alias I've given it but the WHERE clause does not. I've tried going back to the original $_. variable name but they does not seem to work either. When I take the WHERE clause out, I get my output with the aliased headings.

EXAMPLE with no @{Name alias and the WHERE works

PS C:\> Get-WmiObject Win32_operatingsystem | select version,BuildNUmber | where { $_.buildnumber -eq 3790} |  sort version

version                                                                                                BuildNUmber                                                                                           
-------                                                                                                -----------                                                                                           
5.2.3790                                                                                                   3790                 

EXAMPLE with alias and WHERE does not work

Get-WmiObject Win32_operatingsystem | select @{Name="Ver";Expression={$_.version}}, @{Name="Build";Expression={$_.BuildNUmber}} | where { "build" -eq 3790} |  sort "ver" 

EXAMPLE with alias and NO WHERE, gets output. SORT uses alias and works as well.

PS C:\> Get-WmiObject Win32_operatingsystem | select @{Name="Ver";Expression={$_.version}}, @{Name="Build";Expression={$_.BuildNUmber}} |  sort "ver" 

Ver                                                                                                    Build                                                                                                 
---                                                                                                    -----                                                                                                 
5.2.3790                                                                                               3790                                                                                                  

So Ideally I'd like to be able to use the @{name with a WHERE clause..

1 Answers1

1

You have a syntax flaw:

where { "build" -eq 3790} 

The above statement is functionally the same as:

where { $false }

Which would return absolutely nothing, no matter what you piped to it.

Change it to

where { $_.Build -eq 3790 }
Mathias R. Jessen
  • 24,907
  • 4
  • 62
  • 95
  • hahahah Mathias, I talked to a colleague and re-read your question, you are absolutely right. I had tried $_.buildversion and it hadn't worked, $_.build does. I wasn't clear on how SORT didn't require the $_. and BUILD did. Problem solved thanks – user1854377 Feb 10 '15 at 18:50
  • Using a string value with `Sort-Object` works, because the parameter in position 0 (`-Property`) expects one or more strings that match a property name. Glad it makes sense after all ;-) – Mathias R. Jessen Feb 10 '15 at 19:40