Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - What does the $() operator do?

Reply
 
Old 02-08-2008   #1 (permalink)
Kevin Buchan
Guest


 
 

What does the $() operator do?

I have googled and I've used get-help. I've even tried my own tests,
but I can't rell what the $() operator does.

I've seen it in people's code and I think I can tell, from the
context, what it does, but I'm mistaken.

#Example 1:
param(
[string] $wsdlLocation = $(throw "Please specify a WSDL
location"),
)

#Example 2
$s = indent "<$($reader.Name) "


I was feeling like I was finally nearing the crest of the learning
curve, to where I can actually use PowerShell instead of simply
running other people's scripts, then I run into this. :-)

Thanks!

--
Kevin Buchan
kevin.bucahn@xxxxxx[nospam]sanders.com

My System SpecsSystem Spec
Old 02-08-2008   #2 (permalink)
jas
Guest


 
 

Re: What does the $() operator do?

Kevin,

Sometimes you need to evaluate an expression and then use the results to do
something else.

Classicly you would probably do
$myvar = <some code that returns a value/object>
if ($mayvar -eq "<somevalue>") { do something else }

Using $() allows you to do this all in one line.
if($(<some code that returns a value/object>) -eq "<somevalue>") { do
something else }

You will see this a lot in code as people force an expression to be
evaluated, you may even see $($myvar) where $myvar is of a type that returns
an unhelpful object but placing that inside another $() actually takes you
to the value you want. I am sure smarter people can give more technical
answers.

James


"Kevin Buchan" <kevin.buchan@xxxxxx[Pls Don't Spam]sanders.com> wrote in
message newsmhoq3t1na8c4rg3gce6vgji0aoeiloqi2@xxxxxx
Quote:

>I have googled and I've used get-help. I've even tried my own tests,
> but I can't rell what the $() operator does.
>
> I've seen it in people's code and I think I can tell, from the
> context, what it does, but I'm mistaken.
>
> #Example 1:
> param(
> [string] $wsdlLocation = $(throw "Please specify a WSDL
> location"),
> )
>
> #Example 2
> $s = indent "<$($reader.Name) "
>
>
> I was feeling like I was finally nearing the crest of the learning
> curve, to where I can actually use PowerShell instead of simply
> running other people's scripts, then I run into this. :-)
>
> Thanks!
>
> --
> Kevin Buchan
> kevin.bucahn@xxxxxx[nospam]sanders.com
My System SpecsSystem Spec
Old 02-08-2008   #3 (permalink)
Kirk Munro [MVP]
Guest


 
 

Re: What does the $() operator do?

Hi Kevin,

Technically it is called a subexpression. It basically instructs PowerShell
to evaluate it first and use the result of the subexpression wherever the
subexpression was located. This is very useful when building strings using
properties of objects. For example, suppose you needed to include the
department of a user as part of a string, and that your user was in a
variable called $user. You would use subexpressions to do this as follows:

"Department: $($user.department)"

Without the subexpression syntax the .department suffix would be treated as
a literal part of the string.

--
Kirk Munro [MVP]
Poshoholic
http://www.poshoholic.com

"jas" <james.seatter@xxxxxx> wrote in message
news:CB52EE09-F062-4E6A-8EF9-EA663DF36A36@xxxxxx
Quote:

> Kevin,
>
> Sometimes you need to evaluate an expression and then use the results to
> do something else.
>
> Classicly you would probably do
> $myvar = <some code that returns a value/object>
> if ($mayvar -eq "<somevalue>") { do something else }
>
> Using $() allows you to do this all in one line.
> if($(<some code that returns a value/object>) -eq "<somevalue>") { do
> something else }
>
> You will see this a lot in code as people force an expression to be
> evaluated, you may even see $($myvar) where $myvar is of a type that
> returns an unhelpful object but placing that inside another $() actually
> takes you to the value you want. I am sure smarter people can give more
> technical answers.
>
> James
>
>
> "Kevin Buchan" <kevin.buchan@xxxxxx[Pls Don't Spam]sanders.com> wrote in
> message newsmhoq3t1na8c4rg3gce6vgji0aoeiloqi2@xxxxxx
Quote:

>>I have googled and I've used get-help. I've even tried my own tests,
>> but I can't rell what the $() operator does.
>>
>> I've seen it in people's code and I think I can tell, from the
>> context, what it does, but I'm mistaken.
>>
>> #Example 1:
>> param(
>> [string] $wsdlLocation = $(throw "Please specify a WSDL
>> location"),
>> )
>>
>> #Example 2
>> $s = indent "<$($reader.Name) "
>>
>>
>> I was feeling like I was finally nearing the crest of the learning
>> curve, to where I can actually use PowerShell instead of simply
>> running other people's scripts, then I run into this. :-)
>>
>> Thanks!
>>
>> --
>> Kevin Buchan
>> kevin.bucahn@xxxxxx[nospam]sanders.com
>
My System SpecsSystem Spec
Old 02-08-2008   #4 (permalink)
Hans Dingemans
Guest


 
 

Re: What does the $() operator do?

>>> I am sure smarter people can give more technical answers.

Keith Hill is such a guy. I came across a very good explanation of
PowerShell parsing modes and as a sidebar the difference between several
types of expressions!

http://keithhill.spaces.live.com/blo...A97!6058.entry


What's the difference between grouping expressions (), subexpressions $()
and array subexpressions @()? A grouping expression can contain just a
single statement. A subexpression can contain multiple semicolon separated
statements. The output of each statement contributes to the output of the
subexpression. An array subexpression behaves just like a subexpression
except that it guarantees that the output will be an array. The two cases
where this makes a difference are 1) there is no output at all so the result
will be an empy array and 2) the result is a scalar value so the result will
be a single element array containg the scalar value. If the output is
already an array then the use of an array subexpession will have no affect
on the output i.e. array subexpressions do not wrap arrays inside of another
array.



"jas" <james.seatter@xxxxxx> wrote in message
news:CB52EE09-F062-4E6A-8EF9-EA663DF36A36@xxxxxx
Quote:

> Kevin,
>
> Sometimes you need to evaluate an expression and then use the results to
> do something else.
>
> Classicly you would probably do
> $myvar = <some code that returns a value/object>
> if ($mayvar -eq "<somevalue>") { do something else }
>
> Using $() allows you to do this all in one line.
> if($(<some code that returns a value/object>) -eq "<somevalue>") { do
> something else }
>
> You will see this a lot in code as people force an expression to be
> evaluated, you may even see $($myvar) where $myvar is of a type that
> returns an unhelpful object but placing that inside another $() actually
> takes you to the value you want. I am sure smarter people can give more
> technical answers.
>
> James
>
>
> "Kevin Buchan" <kevin.buchan@xxxxxx[Pls Don't Spam]sanders.com> wrote in
> message newsmhoq3t1na8c4rg3gce6vgji0aoeiloqi2@xxxxxx
Quote:

>>I have googled and I've used get-help. I've even tried my own tests,
>> but I can't rell what the $() operator does.
>>
>> I've seen it in people's code and I think I can tell, from the
>> context, what it does, but I'm mistaken.
>>
>> #Example 1:
>> param(
>> [string] $wsdlLocation = $(throw "Please specify a WSDL
>> location"),
>> )
>>
>> #Example 2
>> $s = indent "<$($reader.Name) "
>>
>>
>> I was feeling like I was finally nearing the crest of the learning
>> curve, to where I can actually use PowerShell instead of simply
>> running other people's scripts, then I run into this. :-)
>>
>> Thanks!
>>
>> --
>> Kevin Buchan
>> kevin.bucahn@xxxxxx[nospam]sanders.com
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
-ne operator PowerShell
-f operator PowerShell
64 bit binary or (-bor operator)? PowerShell
Suggestion: 1 new operator (well, maybe 6...) PowerShell
-f Format Operator PowerShell


Vista Forums 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 Ltd

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