Score:0

Format date in the output of a PowerShell query

cn flag

PowerShell nubie question. How do I format the date in a PowerShell query result? Example command:

get-adcomputer -Filter {operatingsystem -like "Windows 10*"} -Properties operatingsystemversion, lastlogon | select-object name, operatingsystemversion, @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}} | select name, operatingsystemversion, lastlogon

The output looks like this:

PCName 10.0 (19045) 4/27/2023 2:30:16 PM

and I'd like to show just the year and month like this:

PCName 10.0 (19045) 2023-04

Score:2
us flag

This should do what you're looking for (at least it works on my machine).

get-adcomputer -Filter {operatingsystem -like "Windows 10*"} -Properties operatingsystemversion, lastlogondate | select-object name, operatingsystemversion, @{n='LastLogon';e={Get-Date $_.lastlogondate -Format yyyy-MM}} | select name, operatingsystemversion, lastlogon
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.