0

I have a PowerShell script I run on a regular basis. Part of the script creates csv files that I later use to import data into various software programs.

The data is in a variable $enrolledStudents and the type is a System.Array

I use the following to export part of the data:

$enrolledStudents | Select-Object @{Name="SFIRST";Expression={$_.FirstName}},`
@{Name="SLAST";Expression={$_.LastName}},`
        @{Name="SGRADE";Expression={[int]$_.GradeLevel}},`
        @{Name="SBIRTHDAY";Expression={$_.Birthdate }} |    
    Export-CSV C:\temp\Exported.csv -notype -Append 

The Export looks like:
"SFIRST","SLAST","SGRADE","SBIRTHDAY"
"John","Doe","6","2009-11-22"

The software I upload the data to needs the date formatted as “11/22/2009” so it would look like: "SFIRST","SLAST","SGRADE","SBIRTHDAY"
"John","Doe","6","11/22/2009"

Is there a way to do this in the Select-Object ?

  • 1
    If ```Birthdate``` is a proper ```DateTime``` object you can just do something like ```Expression={$_.Birthdate.ToString("MM/dd/yyyy") }```. If it's string you'll need to do some cutting and shutting - e.g. ```Expression={ "{1}/{2}/{0}" -f $_.Birthdate.Split("-") }``` – mclayton Oct 26 '22 at 18:29
  • Birthdate is not a DateTime object, it originates from a SQL query, I believe it is a string. The second part is a solution that will work for me. – Ron Johnson Oct 26 '22 at 18:50

2 Answers2

0

Sure thing. Just apply a little object typing magic...

$enrolledStudents | Select-Object `
    @{Name="SFIRST";Expression={$_.FirstName}},`
    @{Name="SLAST";Expression={$_.LastName}},`
    @{Name="SGRADE";Expression={[int]$_.GradeLevel}},`
    @{Name="SBIRTHDAY";Expression={([datetime]$_.Birthdate).ToString('MM/dd/yyyy')}} |
Export-CSV C:\temp\Exported.csv -notype -Append 

Edit: Corrected the date format syntax

Dennis
  • 871
  • 9
  • 29
  • I like this answer but {([datetime]$_.Birthdate).ToString('mm/dd/YYYY')} should be {([datetime]$_.Birthdate).ToString('MM/dd/yyyy')}} I tried using the T0String and failed before I posted the question, I believe I needed [DateTime] This also works – Ron Johnson Oct 26 '22 at 18:57
  • Ha, ha. Didn't check what my output said before posting. Of course you are right :) – Dennis Oct 26 '22 at 19:01
0

I now have two working solutions and wanted to add a note. Both give the same output.

# Thanks mclayton
$enrolledStudents | Select-Object @{Name="SFIRST";Expression={$_.FirstName}},`
    @{Name="SLAST";Expression={$_.LastName}},`
    @{Name="SGRADE";Expression={[int]$_.GradeLevel}},`
    @{Name="SBIRTHDAY";Expression={ "{1}/{2}/{0}" -f $_.Birthdate.split("-")}} |    
    Export-CSV C:\temp\test-Renaissance.csv -notype -Append 

and

# Thanks Dennis
$enrolledStudents | Select-Object @{Name="SFIRST";Expression={$_.FirstName}},`
    @{Name="SLAST";Expression={$_.LastName}},`
    @{Name="SGRADE";Expression={[int]$_.GradeLevel}},`
    @{Name="SBIRTHDAY";Expression={([DateTime]$_.Birthdate).ToString('MM/dd/yyyy')}} |  
    Export-CSV C:\temp\test-Renaissance.csv -notype -Append 

I will use the second because for me, it is easier to read.
In my case it is very Important to have the [DateTime]$__.Birthdate. I found numerous examples using the .ToString but did not come across one that used the [datetime]$_