Hetfield84
Enthusiast
Enthusiast

Select VMs created in 2020 using ExtensionData.Config.createDate

Jump to solution

Hi,

I'm trying to filter out results, to only show VMs that have been built in 2020. The line I am using below doesn't seem to work as it returns no results, however if I remove the where clause it shows plenty of VMs created in 2020. What am I doing wrong here?

Get-VM | Select-Object Name, @{N="created"; E={$_.ExtensionData.Config.createDate}} | where {$_.ExtensionData.Config.createDate -like "*2020*"}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Two issues, the Where-clause should come after the Get-VM.
And the property is of type DateTime, so it easier to compare with another DateTime object.

Get-VM | where {$_.ExtensionData.Config.createDate -ge (Get-Date '1/1/2020')} |

Select-Object Name, @{N="created"; E={$_.ExtensionData.Config.createDate}}


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

View solution in original post

4 Replies
LucD
Leadership
Leadership

Two issues, the Where-clause should come after the Get-VM.
And the property is of type DateTime, so it easier to compare with another DateTime object.

Get-VM | where {$_.ExtensionData.Config.createDate -ge (Get-Date '1/1/2020')} |

Select-Object Name, @{N="created"; E={$_.ExtensionData.Config.createDate}}


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

Hetfield84
Enthusiast
Enthusiast

Thanks that worked! Is there a way to strip out the time and just show the date in the results?

0 Kudos
LucD
Leadership
Leadership

Try like this

Get-VM | where {$_.ExtensionData.Config.createDate -ge (Get-Date '1/1/2020')} |

Select-Object Name, @{N="created"; E={$_.ExtensionData.Config.createDate.ToShortDateString()}}


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

Hetfield84
Enthusiast
Enthusiast

You saved the day once again. Thank you for your help!

0 Kudos