Variable properties

Welcome Forums General PowerShell Q&A Variable properties

This topic contains 12 replies, has 8 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #111155

    Participant
    Points: 30
    Rank: Member

    I'm a newbie at this, forgive the silly question.

    I'm reading Learn Windows PowerShell in a Month of Lunches. In the chapter on variables, it is mentioned that variables have properties, such as count, length, and so on. However I cannot see a way to list the properties available. I tried $myvariable | gm, but that certainly doesn't show the properties.

    Can anyone clarify for me?

    R

  • #111164

    Participant
    Points: 260
    Helping Hand
    Rank: Contributor

    It always depends on what's in your variable.

    $item = Get-Item -Path C:\Windows\explorer.exe
    $item | Get-Member

    Or even more specific:

    $item | Get-Member -MemberType Property

    Or did you want to see the values of the properties?

    $item | Select-Object -Property *
  • #111307

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    Variables themselves don't have properties; the things a variable contains can have properties. An empty variable won't generate results from Get-Member, because there's nothing in the variable for Get-Member to reflect on.

  • #111493

    Participant
    Points: 1
    Rank: Member

    Looks like y'all were on top of this one.. The variable has to be loaded with something first..

     

    [2:32:32 PM] C:/Scripts> $test = "bob"
    [2:32:51 PM] C:/Scripts> $test | gm
    
    
       TypeName: System.String
    
    Name             MemberType            Definition
    ----             ----------            ----------
    Clone            Method                System.Object Clone(), System.Object ICloneable.Clone()
    CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Object obj), in...
    Contains         Method                bool Contains(string value)
    CopyTo           Method                void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
    EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool EndsWith(...
    Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, System.StringComparison com...
    GetEnumerator    Method                System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator(), System.Collec...
    GetHashCode      Method                int GetHashCode()
    GetType          Method                type GetType()
    GetTypeCode      Method                System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
    IndexOf          Method                int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, int startIndex, int co...
    IndexOfAny       Method                int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char[] anyOf, int sta...
    Insert           Method                string Insert(int startIndex, string value)
    IsNormalized     Method                bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)
    LastIndexOf      Method                int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char value, int startI...
    LastIndexOfAny   Method                int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int LastIndexOfAny(char[] an...
    Normalize        Method                string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)
    PadLeft          Method                string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
    PadRight         Method                string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)
    Remove           Method                string Remove(int startIndex, int count), string Remove(int startIndex)
    Replace          Method                string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue)
    Split            Method                string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] sepa...
    StartsWith       Method                bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType), bool Start...
    Substring        Method                string Substring(int startIndex), string Substring(int startIndex, int length)
    ToBoolean        Method                bool IConvertible.ToBoolean(System.IFormatProvider provider)
    ToByte           Method                byte IConvertible.ToByte(System.IFormatProvider provider)
    ToChar           Method                char IConvertible.ToChar(System.IFormatProvider provider)
    ToCharArray      Method                char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
    ToDateTime       Method                datetime IConvertible.ToDateTime(System.IFormatProvider provider)
    ToDecimal        Method                decimal IConvertible.ToDecimal(System.IFormatProvider provider)
    ToDouble         Method                double IConvertible.ToDouble(System.IFormatProvider provider)
    ToInt16          Method                int16 IConvertible.ToInt16(System.IFormatProvider provider)
    ToInt32          Method                int IConvertible.ToInt32(System.IFormatProvider provider)
    ToInt64          Method                long IConvertible.ToInt64(System.IFormatProvider provider)
    ToLower          Method                string ToLower(), string ToLower(cultureinfo culture)
    ToLowerInvariant Method                string ToLowerInvariant()
    ToSByte          Method                sbyte IConvertible.ToSByte(System.IFormatProvider provider)
    ToSingle         Method                float IConvertible.ToSingle(System.IFormatProvider provider)
    ToString         Method                string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString(System.IFormat...
    ToType           Method                System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)
    ToUInt16         Method                uint16 IConvertible.ToUInt16(System.IFormatProvider provider)
    ToUInt32         Method                uint32 IConvertible.ToUInt32(System.IFormatProvider provider)
    ToUInt64         Method                uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
    ToUpper          Method                string ToUpper(), string ToUpper(cultureinfo culture)
    ToUpperInvariant Method                string ToUpperInvariant()
    Trim             Method                string Trim(Params char[] trimChars), string Trim()
    TrimEnd          Method                string TrimEnd(Params char[] trimChars)
    TrimStart        Method                string TrimStart(Params char[] trimChars)
    Chars            ParameterizedProperty char Chars(int index) {get;}
    Length           Property              int Length {get;}
    
  • #111496

    Participant
    Points: 315
    Helping Hand
    Rank: Contributor

    Another important thing to understand is what type of data the variable contains, so the properties will be vary for each of these:

    $bull = "string"
    $bull = 1
    $bull = 1,2,3
    

    That is a true vary a bull. ūüėõ

  • #111499

    Participant
    Points: 30
    Rank: Member

    Thanks for your replies. However, I still have not been able to find it. Here is the example:

    PS C:\> $computers[0]
    SERVER-R2
    PS C:\> $computer[1]
    SERVER1
    PS C:\> $computer[-1]
    localhost
    PS C:\> $computer[-2]
    SERVER1

    PS c:\> $computers.count
    3

    OK, I can see that the variable $computers has a count property, but how can I list the other properties it has so I can choose from them?

    Thanks again

     

     

  • #111500

    Participant
    Points: 260
    Helping Hand
    Rank: Contributor

    To list the members of an object you simply use Get-Member.

    $computers | Get-Member

    The alias for Get-Member is gm.

  • #111508
    js

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor

    format-list or fl is another way. Usually "| fl *" shows a lot more.

    $a = get-process terminal
    $a | fl
    $a | fl *
    
  • #111509

    Participant
    Points: 30
    Rank: Member

    As I mentioned in my original post, the format $computers | gm does not show me the properties, such as the count property in the example. Here is the output of $computers | gm

    PS C:\Users\RogerZ> $computers | gm

    All I get are methods and one property (length).

     

     

    TypeName: System.String

     

    Name             MemberType            Definition

    —-¬† ¬† ¬† ¬† ¬† ¬† ¬†———-¬† ¬† ¬† ¬† ¬† ¬† ———-

    Clone            Method                System.Object Clone(), System.Object IClon...

    CompareTo        Method                int CompareTo(System.Object value), int Co...

    Contains         Method                bool Contains(string value)

    CopyTo           Method                void CopyTo(int sourceIndex, char[] destin...

    EndsWith         Method                bool EndsWith(string value), bool EndsWith...

    Equals           Method                bool Equals(System.Object obj), bool Equal...

    GetEnumerator    Method                System.CharEnumerator GetEnumerator(), Sys...

    GetHashCode      Method                int GetHashCode()

    GetType          Method                type GetType()

    GetTypeCode      Method                System.TypeCode GetTypeCode(), System.Type...

    IndexOf          Method                int IndexOf(char value), int IndexOf(char ...

    IndexOfAny       Method                int IndexOfAny(char[] anyOf), int IndexOfA...

    Insert           Method                string Insert(int startIndex, string value)

    IsNormalized     Method                bool IsNormalized(), bool IsNormalized(Sys...

    LastIndexOf      Method                int LastIndexOf(char value), int LastIndex...

    LastIndexOfAny   Method                int LastIndexOfAny(char[] anyOf), int Last...

    Normalize        Method                string Normalize(), string Normalize(Syste...

    PadLeft          Method                string PadLeft(int totalWidth), string Pad...

    PadRight         Method                string PadRight(int totalWidth), string Pa...

    Remove           Method                string Remove(int startIndex, int count), ...

    Replace          Method                string Replace(char oldChar, char newChar)...

    Split            Method                string[] Split(Params char[] separator), s...

    StartsWith       Method                bool StartsWith(string value), bool Starts...

    Substring        Method                string Substring(int startIndex), string S...

    ToBoolean        Method                bool IConvertible.ToBoolean(System.IFormat...

    ToByte           Method                byte IConvertible.ToByte(System.IFormatPro...

    ToChar           Method                char IConvertible.ToChar(System.IFormatPro...

    ToCharArray      Method                char[] ToCharArray(), char[] ToCharArray(i...

    ToDateTime       Method                datetime IConvertible.ToDateTime(System.IF...

    ToDecimal        Method                decimal IConvertible.ToDecimal(System.IFor...

    ToDouble         Method                double IConvertible.ToDouble(System.IForma...

    ToInt16          Method                int16 IConvertible.ToInt16(System.IFormatP...

    ToInt32          Method                int IConvertible.ToInt32(System.IFormatPro...

    ToInt64          Method                long IConvertible.ToInt64(System.IFormatPr...

    ToLower          Method                string ToLower(), string ToLower(culturein...

    ToLowerInvariant Method                string ToLowerInvariant()

    ToSByte          Method                sbyte IConvertible.ToSByte(System.IFormatP...

    ToSingle         Method                float IConvertible.ToSingle(System.IFormat...

    ToString         Method                string ToString(), string ToString(System....

    ToType           Method                System.Object IConvertible.ToType(type con...

    ToUInt16         Method                uint16 IConvertible.ToUInt16(System.IForma...

    ToUInt32         Method                uint32 IConvertible.ToUInt32(System.IForma...

    ToUInt64         Method                uint64 IConvertible.ToUInt64(System.IForma...

    ToUpper          Method                string ToUpper(), string ToUpper(culturein...

    ToUpperInvariant Method                string ToUpperInvariant()

    Trim             Method                string Trim(Params char[] trimChars), stri...

    TrimEnd          Method                string TrimEnd(Params char[] trimChars)

    TrimStart        Method                string TrimStart(Params char[] trimChars)

    Chars            ParameterizedProperty char Chars(int index) {get;}

    Length           Property              int Length {get;}

  • #111512

    Participant
    Points: 17
    Rank: Member

    I believe the Count property is just like an alias for Lentgh property.  You are correct that it doesn't specifically show "Count" as one of the listed properties when you pipe your variable to Get-Member, but you do see Length property at the bottom of your list.  There's not much else that is not visible which is I believe what you were really trying to ask originally.  If you're wondering what else is there that you cannot see, one thing you can try is to view your variable in the ISE rather than the console.  The intellisense will autosuggest a list of properties when you type the period in $computers.

  • #111514

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor

    Here is a snippet I pulled together a while back to always have handy when looking stuff like this up. Maybe you'll find this useful.

    # Get parameters, examples, full and Online help for a cmdlet or function
    
    # Get a list of all functions
    Get-Command -CommandType Function | 
    Out-GridView -PassThru -Title 'Available functions'
    
    # Get a list of all commandlets
    Get-Command -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available cmdlets'
    
    # Get a list of all functions for the specified name
    Get-Command -Name '*ADGroup*' -CommandType Function | 
    Out-GridView -PassThru -Title 'Available named functions'
    
    # Get a list of all commandlets for the specified name
    Get-Command -Name '*ADGroup**'  -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available named cmdlet'
    
    # get function / cmdlet details
    (Get-Command -Name Get-ADUser).Parameters
    Get-help -Name Get-ADUser -Examples
    Get-help -Name Get-ADUser -Full
    Get-help -Name Get-ADUser -Online
    
    Get-Help about_*
    Get-Help about_Functions
    
    # Find all cmdlets / functions with a target parameter
    Get-Command -CommandType Function | 
    Where-Object { $_.parameters.keys -match 'credential'} | 
    Out-GridView -PassThru -Title 'Available functions which has a specific parameter'
    
    Get-Command -CommandType Cmdlet | 
    Where-Object { $_.parameters.keys -match 'credential'} | 
    Out-GridView -PassThru -Title 'Results for cmdlets which has a specific parameter'
    
    # Get named aliases 
    Get-Alias | 
    Out-GridView -PassThru -Title 'Available aliases'
    
    # Get cmdlet / function parameter aliases
    (Get-Command Get-ADUser).Parameters.Values | 
    where aliases | 
    select Name, Aliases | Out-GridView -PassThru -Title 'Alias results for a given cmdlet or function.'
    
    
    # All Help topics locations
    Get-Help about* | Select Name, Synopsis
    
    Get-Help about* | 
      Select-Object -Property Name, Synopsis |
      Out-GridView -Title 'Select Topic' -OutputMode Multiple |
      ForEach-Object {
        Get-Help -Name $_.Name -ShowWindow
      }
    
    explorer "$pshome\$($Host.CurrentCulture.Name)"
    
    # Get any .NET types and their static methods from PowerShell. 
    # Enumerate all that are currently loaded into your AppDomain.
    #  
    [AppDomain]::CurrentDomain.GetAssemblies() | 
    foreach { $_.GetTypes() } | 
    foreach { $_.GetMethods() } | 
    where { $_.IsStatic } | 
    select DeclaringType, Name | 
    Out-GridView -PassThru -Title '.NET types and their static methods'
    
    # Best Practices
    
    PowerShell scripting best practices
    'https://blogs.technet.microsoft.com/pstips/2014/06/17/powershell-scripting-best-practices'
    
    The Unofficial PowerShell Best Practices and Style Guide
    https://blogs.technet.microsoft.com/pstips/2014/06/17/powershell-scripting-best-practices
    
    Using PSScriptAnalyzer to check your PowerShell code for best practices
    
  • #111517

    Participant
    Points: 260
    Helping Hand
    Rank: Contributor

    If it's for you just about the porperty count try this:

    Get-Member -InputObject $computers

    ūüėČ

  • #111520

    Participant
    Points: 30
    Rank: Member

    Thank you. The ISE trick is pretty useful, as are the snippets. I'll have to dig deeper to see if I can uncover the aliases for the properties. It just seemed strange to me that if a bunch of properties existed as the author said, that there is only one property shown.

    Thanks for all your help.

     

The topic ‘Variable properties’ is closed to new replies.