Format date from Select-Object

Welcome Forums General PowerShell Q&A Format date from Select-Object

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
2 years ago.

  • Author
    Posts
  • #58346

    Participant
    Points: 0
    Rank: Member

    Hello I currently have the code, the results will be exported to a csv file

    $GetInventory = get-childitem -Recurse -file | Select-Object FullName, Name, Extension, LastWriteTime, CreationTime, LastAccessTime, Length
    $GetInventory | Export-CSV $SavePath

    Additionally, depending on the task, the code will include | ? {$_.LastWriteTime -lt (Get-Date).AddYears(-7)}

    What I want to achieve is for the CSV file to output the date in mm\dd\yyyy format, I dont require the time. I have tried the -format D but i'm placing it in the wrong spot.

    " A parameter cannot be found that matches parameter name 'format'."

    How can I achieve the desired results.
    Thank you

  • #58349

    Participant
    Points: 261
    Helping Hand
    Rank: Contributor

    You can use calculated properties to achive this.

    Example:

    ... your code here | Select-Object FullName, Name, Extension, @{Name='LastWriteDate';Expression={Get-Date $_.LastWriteTime -Format 'MM\dd\yyyy'}, CreationTime, LastAccessTime, Length

The topic ‘Format date from Select-Object’ is closed to new replies.