Using a property value in a variable

This topic contains 2 replies, has 2 voices, and was last updated by  Nick Chard 2 years, 1 month ago.

  • Author
  • #34418

    Nick Chard


    A quick question for you experts.

    If I have an object with multiple properties, and I put that object into a variable, I can't always reuse a single property of the object using this format $

    Using $ in a path for example results in the object type being used, rather than the property value. I've come across this problem in few different scenarios, I've copied in an example below. I've tried different combinations of double and single quotes, brackets etc, but the only way I can get it to work is by creating a new variable (as shown below).

    Is there a way to use $ rather than making a new variable each time?

    PS D:\scripts\modules> get-item IIS:\AppPools\* | select -First 1
    Name                     State        Applications                                
    ----                     -----        ------------                                
    .NET v2.0                Started                                                  
    PS D:\scripts\modules> $pool = get-item IIS:\AppPools\* | select -First 1
    PS D:\scripts\modules> $
    .NET v2.0
    PS D:\scripts\modules> Get-ItemProperty IIS:\AppPools\$
    Get-ItemProperty : Cannot find path 'IIS:\AppPools\' because it does not 
    At line:1 char:1
    + Get-ItemProperty IIS:\AppPools\$
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (IIS:\AppPools\ [Get-ItemProperty], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
    PS D:\scripts\modules> $name = $
    PS D:\scripts\modules> Get-ItemProperty IIS:\AppPools\$name
    Name                     State        Applications                                
    ----                     -----        ------------                                
    .NET v2.0                Started                                                  
  • #34419

    random commandline
    Get-ItemProperty $pool.FullName

    or use a sub-expression

    Get-ItemProperty IIS:\AppPools\$($
  • #34459

    Nick Chard

    Thanks. the sub expression is exactly what I was looking for

You must be logged in to reply to this topic.