Extracting only date portion from LastLogonDate

后端 未结 2 1783
借酒劲吻你
借酒劲吻你 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: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.

提交回复
热议问题