VMware Cloud Community
nbhms
Contributor
Contributor
Jump to solution

Insert Date into CSV

I'm using the following command to export information reguarding guest OS disk usage to a CSV file.

Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Get-VMGuest | Select VmName -ExpandProperty Disks | Select VmName, Path, @{ N="DiskSizeGB"; E={ ::Round( ($_.Capacity / 1073741824),0 ) } }, @{ N="FreeSpace"; E={ ::Round( ( $_.FreeSpace / 1073741824),2 ) } }, @{ N="PercFree"; E={ ::Round( ( 100 * ( $_.FreeSpace / $_.Capacity ) ),0 ) } } | Sort VmName | export-csv -path file.csv -notypeinformation

What I'd like to do is add a final column with the date the information was exported . . . any ideas?

Thanks.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could add a hash table in the Select cmdlet and use the Get-Date cmdlet to obtain the date.

With the format operator (-f) you can select in which format you want to display the date.

Get-VM | `
  where { $_.PowerState -eq "PoweredOn" } | `
  Get-VMGuest | `
  Select VmName -ExpandProperty Disks | `
  Select VmName, Path,
    @{ N="Date"; E={("{0:d}" -f (Get-Date))}},
    @{ N="DiskSizeGB"; E={ math::Round( ($_.Capacity / 1073741824),0 ) } }, 
    @{ N="FreeSpace"; E={ math::Round( ( $_.FreeSpace / 1073741824),2 ) } }, 
    @{ N="PercFree"; E={ math::Round( ( 100 * ( $_.FreeSpace / $_.Capacity ) ),0 ) } } |`
  Sort VmName | `
  export-csv -path file.csv -notypeinformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

You could add a hash table in the Select cmdlet and use the Get-Date cmdlet to obtain the date.

With the format operator (-f) you can select in which format you want to display the date.

Get-VM | `
  where { $_.PowerState -eq "PoweredOn" } | `
  Get-VMGuest | `
  Select VmName -ExpandProperty Disks | `
  Select VmName, Path,
    @{ N="Date"; E={("{0:d}" -f (Get-Date))}},
    @{ N="DiskSizeGB"; E={ math::Round( ($_.Capacity / 1073741824),0 ) } }, 
    @{ N="FreeSpace"; E={ math::Round( ( $_.FreeSpace / 1073741824),2 ) } }, 
    @{ N="PercFree"; E={ math::Round( ( 100 * ( $_.FreeSpace / $_.Capacity ) ),0 ) } } |`
  Sort VmName | `
  export-csv -path file.csv -notypeinformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos