How Can We Help?
Use the ‘faster’ methods
The following filtering example with a Where-Object is quite common.
1 |
Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn’} |
Since PowerShell v4 there is now a Where method available. And this method tends to be faster than the equivalent Where-Object cmdlet.
The previous snippet can now be coded as this.
1 |
(Get-VM).Where{$_.PowerState -eq 'PoweredOn'} |
Note that this option is not limited to the Where method, you can also do
1 |
(Get-VM).ForEach('Name’) |
Which is basically the same as
1 |
(Get-VM).Name |
The Where method can also take an addition parameter. This for example a handy way to create two variables, one with powered on and one with powered off VMs.
1 |
$vmOn, $vmOff = (Get-VM).Where( {$_.PowerState -eq 'PoweredOn'}, 'Split') |