Finding attributes for objects

This topic contains 3 replies, has 2 voices, and was last updated by  Doug Morrison 3 years, 9 months ago.

  • Author
    Posts
  • #12957

    Doug Morrison
    Participant

    Hi,

    Still very much a noob with Powershell, but managing to fumble my way through. I'm having some difficulty when it comes to writing some scripts when I want to look at a particular attribute of an object. For example, I have been looking at a script online that looks at the event log for the last logged on user. It returns two attributes, Username and LoginTime. To put it in context;

    $Date = [DateTime]::Now.AddDays(-14)
    $Date.tostring("MM-dd-yyyy"), $env:Computername
    $eventList = @()
    Get-EventLog "Security" -After $Date `
    | Where -FilterScript {$_.EventID -eq 4624 -and $_.ReplacementStrings[4].Length -gt 10 -and $_.ReplacementStrings[5] -eq "dmorrison"} `
    | foreach-Object {
    $row = "" | Select UserName, LoginTime,
    $row.UserName = $_.ReplacementStrings[5]
    $row.LoginTime = $_.TimeGenerated
    $eventList += $row
    }
    $eventList

    My question is, how can I find a list of all available attributes for an object?

    Any help would be appreciated.

    TIA
    Doug

  • #12958

    Dave Wyatt
    Moderator

    Typically, you'd use the Get-Member cmdlet:

    
    $someVariable | Get-Member
    
    # or:
    
    Get-Member -InputObject $someVariable
    
    

    Those two examples are subtly different, if $someVariable happens to be a collection. The first one will pipe the objects in the collection to Get-Member (so you'll see the properties and methods of the items in the collection); the second example will show you the properties and methods of the collection object itself.

    For most types of objects, you can also look them up on MSDN. Use the following command to get the type name, and just plug it into any search engine. The MSDN documentation will usually be right at the top of the list:

    $someVariable.GetType().FullName
    
  • #12962

    Doug Morrison
    Participant

    Awesome, thank you. I knew it was something like that but could only remember get-content. Obviously, I'm still some ways off mastering PS...

    Thanks for the timely response.

  • #13047

    Doug Morrison
    Participant

    To expand on this, is it possible to view the extended properties of an item? For example, the following command will return the default properties, but when I look here, there are loads more properties not listed in the default view.

    get-aduser -identity Username | gm

    TIA

You must be logged in to reply to this topic.