MicrosoftLearning / AZ-040T00-Automating-Administration-with-PowerShell

https://microsoftlearning.github.io/AZ-040T00-Automating-Administration-with-PowerShell/
MIT License
76 stars 96 forks source link

Lab 3A, Ex 2, Task 5, confusion over complex double-negative #26

Closed craigbeeremct closed 2 months ago

craigbeeremct commented 1 year ago

Lab/Demo: Using PowerShell pipeline (the first one)

Exercise 2: Filtering objects

Task 5: Create a report that displays specified Control Panel items

What is this trying to achieve? To list all the items in the System and Security category and in only that category. One item (Power Options) is in two categories, so it should be excluded.

Get-ControlPanelItem -Category "System and Security" `
| where { -not ($PSItem.Category -notlike "*System and Security*") } `
| sort name `
| ft name, category

Since the Category property is a collection (note the braces), there is a much better way of doing this - the Count property. This also avoids the use of a double negative, a frequent source of confusion.

Get-ControlPanelItem -Category "System and Security" `
| where { $PSItem.Category.count -eq 1 } `
| sort name `
| ft name, category

Note that Get-ControlPanelItem is not in PowerShell 7.

TJGitHubUser commented 2 months ago

Closed as resolved.