none
Enumerate and display all properties and values RRS feed

  • Question

  • Hi guys

    Sometimes I am looking for a value/attribute/property in a Powershell variable, but I don't in which attribute/property it is. Then I need to browse through every single attribute and value. Example:

    I want to find an attribute/value named "Month", or a value named "Friday" in the $cdrive variable:

    $cdrive=Get-Item-Pathc:\

    Right now I need to check each attribute of $cdrive to find something related to "Month" or "Friday". I know it would be very easy with this specific example, but I am looking for a way to enumerate through every attribute and its value and display these (maybe export to a text file)

    I hope this makes sense.

    Thanks,

    Michael

    Friday, September 18, 2015 8:38 AM

Answers

  • To expand upon jrv's suggestion, does this help?
    (get-item c:\).PsObject.Properties | format-list @{n='Attribute';e={$psitem.Name}},Value


    Please remember to: "Vote" if my response was helpful. "Mark As Answered" if I answered your question correctly. V/R, Darrick West - Senior Systems Engineer, ConfigMgr: OSD





    • Edited by Darrick West Friday, September 18, 2015 10:59 AM
    • Marked as answer by Michael Skov Tuesday, September 22, 2015 7:05 AM
    Friday, September 18, 2015 10:55 AM

All replies

  • Get-Item c:\ | select *


    \_(ツ)_/

    Friday, September 18, 2015 8:44 AM
  • Yes, but that just gives me the values in the "first" attributes. I want to see attributes and values in all sub attributes.
    Friday, September 18, 2015 8:47 AM
  • Good luck.  You will have to write a script to do that.  You can also download one of the PS object browsers from the repository.

    \_(ツ)_/

    Friday, September 18, 2015 8:53 AM
  • I know, I have tried and failed :-) I was hoping someone had been in a similar situation and made the script.

    Friday, September 18, 2015 8:54 AM
  • (get-item c:\).PsObject.Properties|select -expand value|more


    \_(ツ)_/

    Friday, September 18, 2015 9:02 AM
  • Getting there. But this doesn't work on all variables, as far as I have tested. Second, I would like the attributes and values together, for instance:

    Attribute: Mode

    Value: d--hs

    Attribute: FullName

    Value: C:\

    This is to know where I can find the specific attribute/value

    Friday, September 18, 2015 9:21 AM
  • Close, but with this I will have to manually browse everything, which I am doing already. I would like to display everything so I can see all values and/or search in the results.
    Friday, September 18, 2015 9:23 AM
  • I showed you how to do it.  Now you just need to write your script.


    \_(ツ)_/

    Friday, September 18, 2015 9:36 AM
  • To expand upon jrv's suggestion, does this help?
    (get-item c:\).PsObject.Properties | format-list @{n='Attribute';e={$psitem.Name}},Value


    Please remember to: "Vote" if my response was helpful. "Mark As Answered" if I answered your question correctly. V/R, Darrick West - Senior Systems Engineer, ConfigMgr: OSD





    • Edited by Darrick West Friday, September 18, 2015 10:59 AM
    • Marked as answer by Michael Skov Tuesday, September 22, 2015 7:05 AM
    Friday, September 18, 2015 10:55 AM
  • This is better for sure, thanks.

    However, I still want to get all sub attributes. For instance the CreationTime attribute has Date, Day, DayOfWeek etc. as sub attributes. I would like to see these attributes and their values as well.

    Friday, September 18, 2015 11:00 AM
  • I can do

    ($cdrive.CreationTime).PsObject.Properties |fl@{n='Attribute';e={$_.Name}},Value

    But I would still need to enter each attribute in $cdrive. I am having trouble making a foreach to get them all
    Friday, September 18, 2015 11:14 AM
  • You could try something like this to get you started on a script:

    $cdrive=get-item c:\ #get reference to c:\ object
    $cdrivepropertyname = ($cdrive.PSObject.Properties).Name # get list of properties. you could create a custom list
    $cdrivepropertyname | % { $cdrive.$_ } # this will list all the sub-properties
    
    What's returned still may not go deep enough.

    And you'll of course have to come up with a formatting solution.


    Please remember to: "Vote" if my response was helpful. "Mark As Answered" if I answered your question correctly. V/R, Darrick West - Senior Systems Engineer, ConfigMgr: OSD



    Friday, September 18, 2015 12:47 PM
  • I can do

    ($cdrive.CreationTime).PsObject.Properties |fl@{n='Attribute';e={$_.Name}},Value

    But I would still need to enter each attribute in $cdrive. I am having trouble making a foreach to get them all

    I see no point in what you are trying to do.  I suspect it is driven by a lack of understanding of how to write a PowerShell script.  Perhaps if you stated by learning PowerShell scripting you might see how to write your script.  You are hung on something that is not useful in any scenario that I can think of.


    \_(ツ)_/

    Friday, September 18, 2015 3:50 PM
  • The point is to see all values in an attribute, if I don't know in which attribute the value is. A couple of weeks ago a had to find a specific value, but none of the attribute names had anything to do with the value. After I had searched through almost all attributes, I found the value in a very non-saying attribute.

    In that scenario it would be good to be able to search through all attributes and their values.

    Does that make sense?

    Monday, September 21, 2015 6:31 AM
  • What attributes.  Are you asking about "file attributes"?  You are being very vague. You need to be specific and accurate.  We cannot guess at things if you  use the wrong words or it you make vague references to "search all attributes".  Attributes of what?

    \_(ツ)_/

    Monday, September 21, 2015 6:42 AM
  • Sorry if I am being vague, but I don't know how to explain more than I already have.

    For the sake of example, lets say I want to find a property called "Date" in my $cdrive variable, without browsing all properties manually. I want to search all Properties in my $cdrive variable. I know its very easy in this example, but I have made scripts where the property wasn't easy to find.

    The same goes finding a value. Lets say I want to find a value called "September" in one of my properties. To do this I need to go through all properties. I would like to be able to search all variables under $cdrive to find that value.

    I am working a lot with System Center, and often the values/properties are not very logical to find because of their names. In these cases it would be very good to search through all properties/values.

    Does that make more sense?

    Monday, September 21, 2015 12:18 PM
  • In that scenario it would be good to be able to search through all attributes and their values.

    Why do you need to know? As I posted earlier, if you don't know what you're going to do with the answer to a question, there's no point in making others work hard to answer it.


    -- Bill Stewart [Bill_Stewart]

    Monday, September 21, 2015 12:48 PM
    Moderator
  • Because I don't want to browse through every single property to find the value I am looking for. I apologize if I am not being clear enough. Please check my latest answer for more clarification.

    Monday, September 21, 2015 12:59 PM
  • I don't understand the purpose either.

    You can get members of an object by piping the object instance to Get-Member.

    If an object member is itself an object, you will need to get an instance of that object and pipe to Get-Member, or look up the object in the .NET documentation.

    As I said earlier, if you don't know what you're going to do with the answer to a question, there's not much point in making others work hard to answer it.


    -- Bill Stewart [Bill_Stewart]

    Monday, September 21, 2015 2:17 PM
    Moderator
  • I don't know why this is so hard to explain/doesn't make sense to you guys, but I will keep doing it the way I always have.

    Although I haven't got an answer I will close this thread.

    Tuesday, September 22, 2015 7:05 AM
  • I don't know why this is so hard to explain/doesn't make sense to you guys

    You might consider that there could be something wrong with the question.

    Like I mentioned before, PowerShell can enumerate the members of an object by piping it to Get-Member.

    If a member is an object, pipe that object to Get-Member. You can also look up .NET objects in the .NET documentation.

    That's what everyone else usually does. I'm not sure why you're wanting to do something different.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 22, 2015 2:13 PM
    Moderator
  • I do - I tried explaining myself several times. I know the use of Get-Member, but I would need to do that on every single property, which I want to avoid.

    I have talked to a lot of people who thought this would be a nice thing.

    Tuesday, September 22, 2015 2:17 PM
  • I do - I tried explaining myself several times. I know the use of Get-Member, but I would need to do that on every single property, which I want to avoid.

    But you don't need to. Just look up what you need. There is no real purpose in returning every possible property of every possible object, other than "I just want to see all of it." Just examine the one that's of interest.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 22, 2015 2:20 PM
    Moderator
  • What do you do if you want to find a value named "September", but none of the properties in your variable have anything to do with dates? Then you need to check the value of all properties in your variable, is that correct?

    Tuesday, September 22, 2015 2:23 PM
  • The question seems to reveal a misunderstanding. Date objects don't store the word "September" as that is a string.

    If you want to find string properties of an object, pipe to Get-Member and get object members that have a definition of string. For example:


    PS C:\> Get-Item File.txt | Get-Member | Where-Object { $_.Definition -like '^String' }
    

    This will tell you object properties of a FileInfo object that are strings.

    If you are looking for object properties that contain dates: Same thing, except look for DateTime:


    PS C:\> Get-Item File.txt | Get-Member | Where-Object { $_.Definition -like '^DateTime' }
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 22, 2015 2:46 PM
    Moderator
  • I do - I tried explaining myself several times. I know the use of Get-Member, but I would need to do that on every single property, which I want to avoid.

    I have talked to a lot of people who thought this would be a nice thing.

    You are the second person in a week to ask this. It is a clear indication of a lack of understanding of the technology, of programming and is a totally unnecessary request in all real world situations.

    I challenge you to state one real world situation where this is a requirement.

    When many non-technical users first learn about objects they make an incorrect assumption that objects are like a database.  They are not.  The assumption that we can query a hierarchy as in SQL is incorrect. We cannot use SQL-like constructs to access objects.

    Once you have enough experience with PowerShell, Windows and programming this will be easier to understand.  Until you learn PowerShell to a level of reasonable proficiency the answers here may be inaccessible to you.


    \_(ツ)_/

    Tuesday, September 22, 2015 3:01 PM
  • "September" is only an example. Another example: I want to find any values with "Monday" (which is stored in DayOfWeek). If i can get a tree structure with all properties and their values exported to a text file, i could see in which property i could find "Monday".

    Right now i need to check every property to see if they contain the info i want.

    Get-Member requires me to put every object in front of | Get-Member, which is what i want to avoid, as it is a lot of manual work.

    Note that i don't expect anyone to do some heavy work for me! I know several people (including developers) who would like such a script a lot. In 80% of my cases i can find my info quite fast, but for some cases the values i look for are placed in a very non-descriptive property.

    Wednesday, September 23, 2015 1:43 PM
  • Again: [DateTime] objects don't store a string "Monday". The day of week is a .NET enumeration. For example:


    PS C:\> [Enum]::GetValues([DayofWeek])

    This is localized depending on your OS culture.

    jrv is correct. Objects are not a database you can query. That's not how it's designed to work. Instead, simply look up the information for the object you're interested in.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 23, 2015 2:24 PM
    Moderator
  • "September" is only an example. Another example: I want to find any values with "Monday" (which is stored in DayOfWeek). If i can get a tree structure with all properties and their values exported to a text file, i could see in which property i could find "Monday".

    Right now i need to check every property to see if they contain the info i want.

    Get-Member requires me to put every object in front of | Get-Member, which is what i want to avoid, as it is a lot of manual work.

    Note that i don't expect anyone to do some heavy work for me! I know several people (including developers) who would like such a script a lot. In 80% of my cases i can find my info quite fast, but for some cases the values i look for are placed in a very non-descriptive property.

    This is a fundamental reasonyou cannot understand this.  Nothing is "stored" in a date object.  A date object is a structure containing the date value.  The methods an property methods are used to dynamically extract information from the stored value.  The stored value is a number.  datetime I not an object in the classic sense. It is a System.ValueType like an integer or a double.

    A datetime is a simple number which you can see and use:

    [datetime]0 is the beginning of time "1/1/0001 00:00".  "Now" is the number of ticks since the beginning of time: "[datetime]::Now.Ticks".  This is the raw number that is stored.  DayOfWeek is a calculation that is made everytime you reference it.  The result of the calculation is a number which is then converted through the enum into a displayed text based on culture.

    Once you learn how all of these things are built you will begin to see why your questions are kind of way off of the mark.

    Learning all of this takes time.  If you need to understand then you will need to go back and study the fundamentals of computers and computer automation as well as learning modern programming systems.  Object systems are diverse and MS has implemented but one flavor of an object system in the Net Framework.


    \_(ツ)_/

    Wednesday, September 23, 2015 4:07 PM
  • Hi guys

    Sometimes I am looking for a value/attribute/property in a Powershell variable, but I don't in which attribute/property it is. Then I need to browse through every single attribute and value. Example:

    I want to find an attribute/value named "Month", or a value named "Friday" in the $cdrive variable:

    $cdrive=Get-Item-Pathc:\

    Right now I need to check each attribute of $cdrive to find something related to "Month" or "Friday". I know it would be very easy with this specific example, but I am looking for a way to enumerate through every attribute and its value and display these (maybe export to a text file)

    I hope this makes sense.

    Thanks,

    Michael

    Learn about the cmdlet named  ConvertTo-Json

    <##> Get-Item -Path c:\ | ConvertTo-Json -depth 1
    {
        "Name":  "C:\\",
        "Parent":  null,
        "Exists":  true,
        "Root":  {
                     "Name":  "C:\\",
                     "Parent":  null,
                     "Exists":  true,
                     "Root":  "C:\\",
                     "FullName":  "C:\\",
                     "Extension":  "",
                     "CreationTime":  "\/Date(1247539136526)\/",
                     "CreationTimeUtc":  "\/Date(1247539136526)\/",
                     "LastAccessTime":  "\/Date(1442873899212)\/",
                     "LastAccessTimeUtc":  "\/Date(1442873899212)\/",
                     "LastWriteTime":  "\/Date(1442873899212)\/",
                     "LastWriteTimeUtc":  "\/Date(1442873899212)\/",
                     "Attributes":  22
                 },
        "FullName":  "C:\\",
        "Extension":  "",
        "CreationTime":  "\/Date(1247539136526)\/",
        "CreationTimeUtc":  "\/Date(1247539136526)\/",
        "LastAccessTime":  "\/Date(1442873899212)\/",
        "LastAccessTimeUtc":  "\/Date(1442873899212)\/",
        "LastWriteTime":  "\/Date(1442873899212)\/",
        "LastWriteTimeUtc":  "\/Date(1442873899212)\/",
        "Attributes":  22,
        "PSPath":  "Microsoft.PowerShell.Core\\FileSystem::C:\\",
        "PSParentPath":  "",
        "PSChildName":  "C:\\",
        "PSDrive":  {
                        "CurrentLocation":  "Users\\Larry\\Documents\\WindowsPowerShell",
                        "Name":  "C",
                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                        "Root":  "C:\\",
                        "Description":  "OS",
                        "Credential":  "System.Management.Automation.PSCredential",
                        "DisplayRoot":  null
                    },
        "PSProvider":  {
                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                           "Name":  "FileSystem",
                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                           "ModuleName":  "Microsoft.PowerShell.Core",
                           "Module":  null,
                           "Description":  "",
                           "Capabilities":  52,
                           "Home":  "C:\\Users\\Larry",
                           "Drives":  "C D F"
                       },
        "PSIsContainer":  true,
        "BaseName":  "C:\\",
        "Mode":  "d--hs"
    }

    Thursday, September 24, 2015 2:40 AM