PowerTip: Finding the Type of an Object

PowerTip: Finding the Type of an Object

  • Comments 6
  • Likes

Question: What .NET Framework class is leveraged by the Get-EventLog cmdlet?

Answer: System.Diagnostics.EventLogEntry

Question: How would I find the above information?

Answer: Get-EventLog application | get-member

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • If we run that solution on a large application log we might end up waiting all day for the output to end.  This happens if we are running over an RDP link.  The control keys so not work as expected.  Ctrl-C does not terminate output.  We could be forced to abort the PowerShell session (Ctlrl-Break).

    Try this as it is cleaner:

    (get-eventlog -Newest 1 -LogName application).GetType()

    (get-process system).GetType()

    (Get-Service browser).GetType()

    (get-wmiobject win32_bios).GetType()

    Design a command that returns only one example and use GetType()

    I use GM when I already have an exemplar and want to inspect it.

    $p=Get-Process w*

    $p[0].GetType()

    $p[0]|gm

    Many ways to do similar things.

  • you can also explore typenames:

    (Get-EventLog -LogName application -Newest 1).pstypenames

  • @JRV you are absolutely correct, and I suspect that one would only make that "mistake" once ... and now maybe not even once with your suggestion! When working locally, if I need to find information about an object, I often use the same code that I am going to use later, and therefore do not modify it to only return a single instance. Then when the information comes pouring in, I press CTRL-C to break up the returning information, so I can "quickly" get the information. Certainly it is better to use one of your solutions, but that does require a lot of extra typing. Depending on what I am doing, I will use GetType as you suggest.

  • @Walid Toumi  that is a great suggestion, and a technique I had forgotten about. Thank you for sharing.

  • @?? Red Med ORed.

    Locally no issue.  Just be carefully remoely.

  • @jrv: You are absolutely right!

    And I really agree that this should be considered!

    Klaus.