Howto format datevalues where result contains several date fields

36 Views Asked by At
'user1','user2' | foreach-object { 
    get-aduser -identity $_ -Properties * | 
    select name,DisplayName,samaccountname,enabled, accountexpirationdate,whenchanged }| ft


name   DisplayName  samaccountname enabled accountexpirationdate whenchanged        
----   -----------  -------------- ------- --------------------- -----------        
user1, MyName,       user1          False 11-04-2023 00:00:00   11-04-2023 00:42:21
user2, MyName,       user2          False 11-04-2023 00:00:00   11-04-2023 00:42:21

I want to have all the date fields formatted as shortdates

I have tried to add -format string to each datefield without success

1

There are 1 best solutions below

0
Keith Langmead On

This should do what you're looking for

'user1','user2' | foreach-object { 
    get-aduser -identity $_ -Properties * | 
    select name,DisplayName,samaccountname,enabled,@{n='ExpirationDate';e={Get-Date $_.accountexpirationdate -Format 'dd-MM-yy'}},@{n='WhenChanged';e={Get-Date $_.whenchanged -Format 'dd-MM-yy'}} 
}| ft

where you can tweak the exact date format being returned by changing the format bit in each

@{n='ExpirationDate';e={Get-Date $_.accountexpirationdate -Format 'dd-MM-yy'}}

entry as needed.