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 - Set-Variable

Reply
 
Old 10-25-2006   #1 (permalink)
Fred J.


 
 

Set-Variable

PS> Set-Variable -name xy -value "orange"
PS> Set-Variable -name xy -description "this variable contains a color"
PS> Get-Variable -name xy
Name Value
---- -----
xy orange

PS> Get-Variable -name xy |format-list -property *
Name : xy
Description : this variable contains a color
Value : orange
Options : None
Attributes : {}

PS> get-variable xy | format-list -property description
Description : this variable contains a color

Why isn't a value returned when I do this? All I get is a prompt.
PS > $xy.description
PS >
Fred Jacobowitz


My System SpecsSystem Spec
Old 10-25-2006   #2 (permalink)
Matt Hamilton


 
 

Re: Set-Variable

Fred J. wrote:
> PS> Set-Variable -name xy -value "orange"
> PS> Set-Variable -name xy -description "this variable contains a color"
> PS> Get-Variable -name xy
> Name Value
> ---- -----
> xy orange
>
> PS> Get-Variable -name xy |format-list -property *
> Name : xy
> Description : this variable contains a color
> Value : orange
> Options : None
> Attributes : {}
>
> PS> get-variable xy | format-list -property description
> Description : this variable contains a color
>
> Why isn't a value returned when I do this? All I get is a prompt.
> PS > $xy.description


What you're asking for there is the value of a "description" property of
whatever $xy points to.

If $xy was a DateTime then you can do this:

PS > $xy.Year
2006

But in your case $xy is a string ("orange"), and System.String doesn't
have a property called Description.

I guess get-variable is the only way to get to this "metadata" attached
to a variable in PS.
My System SpecsSystem Spec
Old 10-25-2006   #3 (permalink)
Keith Hill [MVP]


 
 

Re: Set-Variable

"Fred J." <swim.instructor@gmail.com> wrote in message
news:1161749627.048671.242200@m7g2000cwm.googlegroups.com...
> Why isn't a value returned when I do this? All I get is a prompt.
> PS > $xy.description


FYI you can also access the description like this:

6# (Get-Variable xy).Description
this variable contains a color

However to answer you question as to why "$xy" didn't work as you expected,
when you access the variable directly using the syntax "$xy" you are telling
PowerShell that you just want the "value" portion of the variable. You can
confirm this by executing:

> $xy | get-member


You will see that the object emitted by $xy is just a string. If you had
executed "Set-Variable -name xy -value 3.14" then $xy would emit a double.
Now when you execute "Get-Variable xy" you will get the full PowerShell
PSVariable object with all the information about the variable: name, value,
description. For example:

13# Get-Variable xy | gm

TypeName: System.Management.Automation.PSVariable

Name MemberType Definition
---- ---------- ----------
Equals Method System.Boolean Equals(Object obj)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
get_Attributes Method
System.Collections.ObjectModel.Collection`1[[Sy...
get_Description Method System.String get_Description()
get_Name Method System.String get_Name()
get_Options Method System.Management.Automation.ScopedItemOptions
....
get_Value Method System.Object get_Value()
IsValidValue Method System.Boolean IsValidValue(Object value)
set_Description Method System.Void set_Description(String value)
set_Options Method System.Void set_Options(ScopedItemOptions
value)
set_Value Method System.Void set_Value(Object value)
ToString Method System.String ToString()
Attributes Property
System.Collections.ObjectModel.Collection`1[[Sy...
Description Property System.String Description {get;set;}
Name Property System.String Name {get;}
Options Property System.Management.Automation.ScopedItemOptions
....
Value Property System.Object Value {get;set;}
OpenHelpTopic ScriptMethod System.Object OpenHelpTopic();

HTH,
Keith


My System SpecsSystem Spec
Old 10-25-2006   #4 (permalink)
George Xie [MSFT]


 
 

Re: Set-Variable

In,

PS> get-variable xy | format-list -property description

get-variable xy will return an object of type PSVariable (representing the
variable itself). Description is a property of this object as you set. That
is why you can get the description property.

But in,

PS> $xy.Description

$xy actually will return the value of variable $xy, which is string
"orange". It doesn't have a description property. That is why you got
nothing back.

You should be getting the same thing with following command,

PS> $xy | format-list -property description

--
George Xie [MSFT]
Microsoft Command Shell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Fred J." <swim.instructor@gmail.com> wrote in message
news:1161749627.048671.242200@m7g2000cwm.googlegroups.com...
> PS> Set-Variable -name xy -value "orange"
> PS> Set-Variable -name xy -description "this variable contains a color"
> PS> Get-Variable -name xy
> Name Value
> ---- -----
> xy orange
>
> PS> Get-Variable -name xy |format-list -property *
> Name : xy
> Description : this variable contains a color
> Value : orange
> Options : None
> Attributes : {}
>
> PS> get-variable xy | format-list -property description
> Description : this variable contains a color
>
> Why isn't a value returned when I do this? All I get is a prompt.
> PS > $xy.description
> PS >
> Fred Jacobowitz
>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
$Variable Is In {1,2,3,4} PowerShell
Ren file with variable PowerShell
Import-CSV from Web/Variable PowerShell
tab and variable PowerShell
How can I ensure that a variable is a built-in powershell variable? 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