i have this powershell that i am looking to tweak so it only gives me lines with a snapshot and timestamp. At the moment its giving me all virtual machines, with or without a snapshot. I just want to filter it so i get VMs with a snapshot only and associated timestamp, i guess which ever will work to filter for VMs with a snapshot
Get-VM | Format-Table -AutoSize VMName, @{Name="TotalSnapshots";Expression={(Get-VMSnapshot -VM $_ | Measure-Object).Count}},@{Name="TimeStamp";Expression={(Get-VMSnapshot -VM $_ | Select-Object CreationTime).CreationTime}}
CodePudding user response:
Use your calculated properties with Select-Object
instead of Format-Table
, which allows you to filter by these properties via Where-Object
:
Get-VM |
Select-Object VMName, @{Name="TotalSnapshots";Expression={(Get-VMSnapshot -VM $_ | Measure-Object).Count}},@{Name="TimeStamp";Expression={(Get-VMSnapshot -VM $_).CreationTime}} |
Where-Object TotalSnapshots -gt 0 |
Format-Table -AutoSize
Note that Select-Object
constructs and outputs data objects, whereas Format-Table
, as all Format-*
cmdlets do, outputs objects that are formatting instructions, which are only meaningful to PowerShell's for-display output-formatting system - see this answer for more information.