Author Posts

February 5, 2014 at 7:48 am

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

February 5, 2014 at 7:57 am

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

February 5, 2014 at 8:19 am

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.

February 12, 2014 at 3:20 am

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