Extracting only date portion from LastLogonDate

后端 未结 2 1753
借酒劲吻你
借酒劲吻你 2021-01-24 13:18

I want to be able to isolate the date from the output of this Get-ADUser command:

Get-ADUser -identity johnd -properties LastLogonDate | Select-Object name, Last         


        
相关标签:
2条回答
  • 2021-01-24 13:48

    Can you use variables? If so,

    PS>$hi=Get-ADuser -identity johnd -properties LastLogonDate|select-object name,LastLogonDate
    PS>$hi.LastLogonDate.ToShortDateString()
    3/21/2016
    PS>$hi.name
    John Doe
    
    0 讨论(0)
  • 2021-01-24 13:50

    The result of that cmdlet is an object with a set of properties. The output you see in table format is not what is literally contained in the object; it's a display representation of it.

    So to first get the date object only, you can modify your Select-Object call (which is already paring down the properties) like this:

    $lastLogon = Get-ADUser -identity johnd -properties LastLogonDate | 
        Select-Object -ExpandProperty LastLogonDate
    

    $lastLogon now contains a [DateTime] object.

    With that you can format it using format strings:

    $lastLogon.ToString('MM/dd/yyyy')
    

    Or even better:

    $lastLogon.ToShortDateString()
    

    (these are slightly different representations; the latter doesn't zero-pad).

    The format strings give you complete control over the representation.

    0 讨论(0)
提交回复
热议问题