Swimburger
Swimburger

Reputation: 7164

How to write a deep where clause in PowerShell

I'm trying to write a where clause in PowerShell that checks if the $root contains a basetemplate with an ID equal to the $template. I can access the basetemplates on items using $item.Template.BaseTemplates[x].ID.

In C# I would be able to write something like this

items.Where(item => item.BaseTemplates.Any(template => template.ID == "id");

So how would I translate this into PowerShell?

$root = Get-Item .
$template = "{E54BB0A6-C296-4D35-BE6A-93E71E2B9F52}"
#Write-Host $root.Template.BaseTemplates[0].ID
$items = Get-ChildItem -recurse -Path $root.FullPath -Language *

#how to do the same query on these $items?
$filteredItems = items | Where-Object { $_.Template.BaseTemplates???.ID -eq $template}

Upvotes: 2

Views: 419

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

Use -in to filter all templates that basetemplates contains your $template:

$filteredItems = items | Where-Object { $template -in $_.Template.BaseTemplates.ID}

Upvotes: 2

Related Questions