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

Why is the precedence for the property operator not being honored

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-08-2007   #1 (permalink)
Bob Landau
Guest


 

Why is the precedence for the property operator not being honored

In the below statement Powershell I think should be binding the $_ to
DeviceID and then evaluating the variable. What its doing is expanding the $_
and concatenating the .DeviceID.

Is the current behavior what is expected?

Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount of
free space on drive $_.DeviceID is $_.FreeSpace"}}



thx
bob

My System SpecsSystem Spec
Old 09-08-2007   #2 (permalink)
Brandon Shell
Guest


 

Re: Why is the precedence for the property operator not being honored

Try this... you have to put $( ) around Variables to let them expand.

Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount of
free space on drive $($_.DeviceID) is $($_.FreeSpace)"}}

"Bob Landau" <BobLandau@xxxxxx> wrote in message
news:8B55372C-692B-4C35-BA1D-664BE3BD3B06@xxxxxx
Quote:

> In the below statement Powershell I think should be binding the $_ to
> DeviceID and then evaluating the variable. What its doing is expanding the
> $_
> and concatenating the .DeviceID.
>
> Is the current behavior what is expected?
>
> Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount
> of
> free space on drive $_.DeviceID is $_.FreeSpace"}}
>
>
>
> thx
> bob
My System SpecsSystem Spec
Old 09-08-2007   #3 (permalink)
Brandon Shell
Guest


 

Re: Why is the precedence for the property operator not being honored

Oh... another option
Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3){"The amount of
free space on drive {0} is {1}" -f ($_.DeviceID),($_.FreeSpace)}}

More Info about the -f operator here
http://bsonposh.com/modules/wordpress/?p=35

"Bob Landau" <BobLandau@xxxxxx> wrote in message
news:8B55372C-692B-4C35-BA1D-664BE3BD3B06@xxxxxx
Quote:

> In the below statement Powershell I think should be binding the $_ to
> DeviceID and then evaluating the variable. What its doing is expanding the
> $_
> and concatenating the .DeviceID.
>
> Is the current behavior what is expected?
>
> Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount
> of
> free space on drive $_.DeviceID is $_.FreeSpace"}}
>
>
>
> thx
> bob
My System SpecsSystem Spec
Old 09-08-2007   #4 (permalink)
Keith Hill
Guest


 

Re: Why is the precedence for the property operator not being honored

"Brandon Shell" <tshell.mask@xxxxxx> wrote in message
news:eoDTQkj8HHA.4880@xxxxxx
Quote:

> Try this... you have to put $( ) around Variables to let them expand.
>
> Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount
> of free space on drive $($_.DeviceID) is $($_.FreeSpace)"}}
The thing to keep in mind here is double-quoted strings in PowerShell will
do variable expansion i.e. $_ is expanded to the string representation of a
Win32_LogicalDisk instance:

\\KEITH1\root\cimv2:Win32_LogicalDisk.DeviceID="C:"

However double-quoted string expansion doesn't support evaluating an
expression by default, which is what accessing properties of a variable
would require. You probably wouldn't want this anyway otherwise something
like this:

"The date is $date."

would error because there is a property operator (.) after the $date object
but you specified no property. So as Brandon points out, the $() in a
string allows you evaluate one or more expressions within the string and the
results are converted to their string representations and placed into the
string.

BTW it is pretty easy to get tripped up on this. I used to do it a lot in
my scripts. I think I'm finally getting to the point where I don't make
this mistake very often.

--
Keith

My System SpecsSystem Spec
Old 09-08-2007   #5 (permalink)
Bob Landau
Guest


 

Re: Why is the precedence for the property operator not being hono

Thank you Brandon,

I like both of your ideas. But I still don't understand whether the current
behavior is expected or not. It seems a shame to need to require anything
special simply to get the value of a property.

Both the QuickStart wiki and "Windows Powershell in Action" both state the
member operator is high up on the order of precedence.


"Brandon Shell" wrote:
Quote:

> Oh... another option
> Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3){"The amount of
> free space on drive {0} is {1}" -f ($_.DeviceID),($_.FreeSpace)}}
>
> More Info about the -f operator here
> http://bsonposh.com/modules/wordpress/?p=35
>
> "Bob Landau" <BobLandau@xxxxxx> wrote in message
> news:8B55372C-692B-4C35-BA1D-664BE3BD3B06@xxxxxx
Quote:

> > In the below statement Powershell I think should be binding the $_ to
> > DeviceID and then evaluating the variable. What its doing is expanding the
> > $_
> > and concatenating the .DeviceID.
> >
> > Is the current behavior what is expected?
> >
> > Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount
> > of
> > free space on drive $_.DeviceID is $_.FreeSpace"}}
> >
> >
> >
> > thx
> > bob
>
>
My System SpecsSystem Spec
Old 09-08-2007   #6 (permalink)
Brandon Shell
Guest


 

Re: Why is the precedence for the property operator not being hono

Keith just explained it well... it is expected.

"Bob Landau" <BobLandau@xxxxxx> wrote in message
news:E5AE901A-EADF-4EF6-B815-21992E492DFA@xxxxxx
Quote:

> Thank you Brandon,
>
> I like both of your ideas. But I still don't understand whether the
> current
> behavior is expected or not. It seems a shame to need to require anything
> special simply to get the value of a property.
>
> Both the QuickStart wiki and "Windows Powershell in Action" both state the
> member operator is high up on the order of precedence.
>
>
> "Brandon Shell" wrote:
>
Quote:

>> Oh... another option
>> Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3){"The amount
>> of
>> free space on drive {0} is {1}" -f ($_.DeviceID),($_.FreeSpace)}}
>>
>> More Info about the -f operator here
>> http://bsonposh.com/modules/wordpress/?p=35
>>
>> "Bob Landau" <BobLandau@xxxxxx> wrote in message
>> news:8B55372C-692B-4C35-BA1D-664BE3BD3B06@xxxxxx
Quote:

>> > In the below statement Powershell I think should be binding the $_ to
>> > DeviceID and then evaluating the variable. What its doing is expanding
>> > the
>> > $_
>> > and concatenating the .DeviceID.
>> >
>> > Is the current behavior what is expected?
>> >
>> > Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The
>> > amount
>> > of
>> > free space on drive $_.DeviceID is $_.FreeSpace"}}
>> >
>> >
>> >
>> > thx
>> > bob
>>
>>
My System SpecsSystem Spec
Old 09-08-2007   #7 (permalink)
Bob Landau
Guest


 

Re: Why is the precedence for the property operator not being hono

Thanks,

I of course hadn't thought about the even more common usage you described
below.

bob


"Keith Hill" wrote:
Quote:

> "Brandon Shell" <tshell.mask@xxxxxx> wrote in message
> news:eoDTQkj8HHA.4880@xxxxxx
Quote:

> > Try this... you have to put $( ) around Variables to let them expand.
> >
> > Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount
> > of free space on drive $($_.DeviceID) is $($_.FreeSpace)"}}
>
> The thing to keep in mind here is double-quoted strings in PowerShell will
> do variable expansion i.e. $_ is expanded to the string representation of a
> Win32_LogicalDisk instance:
>
> \\KEITH1\root\cimv2:Win32_LogicalDisk.DeviceID="C:"
>
> However double-quoted string expansion doesn't support evaluating an
> expression by default, which is what accessing properties of a variable
> would require. You probably wouldn't want this anyway otherwise something
> like this:
>
> "The date is $date."
>
> would error because there is a property operator (.) after the $date object
> but you specified no property. So as Brandon points out, the $() in a
> string allows you evaluate one or more expressions within the string and the
> results are converted to their string representations and placed into the
> string.
>
> BTW it is pretty easy to get tripped up on this. I used to do it a lot in
> my scripts. I think I'm finally getting to the point where I don't make
> this mistake very often.
>
> --
> Keith
>
My System SpecsSystem Spec
Old 09-08-2007   #8 (permalink)
Kiron
Guest


 

Re: Why is the precedence for the property operator not being honored

Alternatively you could concatenate the string:

Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount of
free space on drive " + $_.DeviceID + " is " + $_.FreeSpace}}

--
Kiron

My System SpecsSystem Spec
Old 09-08-2007   #9 (permalink)
Shay Levi
Guest


 

Re: Why is the precedence for the property operator not being honored

Some more examples...

# When -format operator is used you can remove the braces on ($_.property)
Get-WmiObject Win32_logicaldisk | foreach {if ($_.DriveType -eq 3){"The amount
of free space on drive {0} is {1}" -f $_.DeviceID,$_.FreeSpace}}

#narrow down returned properties using the where clause (aka server side
processing)
Get-WmiObject -query "select DeviceID,FreeSpace from Win32_logicaldisk where
DriveType = 3" | foreach {"Free space on drive {0} is {1}" -f $_.DeviceID,$_.FreeSpace}

#narrow down returned properties using the -filter parameter (server side
processing)
Get-WmiObject -class Win32_logicaldisk -filter "DriveType = 3" | foreach
{"Free space on drive {0} is {1}" -f $_.DeviceID,$_.FreeSpace}


Shay
http://scriptolog.blogspot.com


Quote:

> In the below statement Powershell I think should be binding the $_ to
> DeviceID and then evaluating the variable. What its doing is expanding
> the $_ and concatenating the .DeviceID.
>
> Is the current behavior what is expected?
>
> Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The
> amount of free space on drive $_.DeviceID is $_.FreeSpace"}}
>
> thx
> bob

My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
-f operator Tibor Soos PowerShell 3 05-28-2008 07:36 AM
Use my custom TypeDescriptor to obtains default Value on property inXAML Property Editor of Visual Studio 2008 azerty Avalon 0 04-14-2008 06:14 AM
What does the $() operator do? Kevin Buchan PowerShell 3 02-08-2008 02:05 PM
Gotcha: unexpected -or and -and precedence Roman Kuzmin PowerShell 9 06-29-2007 11:04 AM
Windows Media Player - DRM for downloaded content not honored ? musctpmd9 Vista music pictures video 11 01-08-2007 08:50 PM


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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51