This project has moved. For the latest updates, please go here.



The New-XPathQueryGroup cmdlet create a new group of query objects that can be used to create an XPath expression using New-XPathExpression, or an element in an XPath query group using New-XPathQueryGroup. The resulting XPathExpression can be passed as a parameter to Search-Resources.
An XPath query group contains one or more Xpath query objects or other XPathQueryGroup objects.


New-XpathQueryGroup [-Operator] <GroupOperator> {And | Or} [[-Queries] <IXPathQueryObject[]>]  



Required. Specifies the logical operator to use when comparing queries within the group. The support values are And and Or


Required. One or more query objects generated with the New-XPathQuery or New-XPathQueryGroup cmdlets


Viewing the query group string

The New-XPathQueryGroup does not return a usable query itself. It returns only a part of the query that is used in an XPath expression. You can view the query component itself as shown in the following example.
$query1 = New-XPathQuery -AttributeName "AccountName" -Operator Equals -Value "ryan"
$query2 = New-XPathQuery -AttributeName "AccountName" -Operator Equals -Value "bob"
$queryGroup = New-XPathQueryGroup -Operator Or -Queries @($query1, $query2)
$queryGroup .ToString()

This returns an string representing the query component
((AccountName = 'ryan') or (AccountName = 'bob'))

Building a simple query

In order to use the query, it must be rendered by an XPath expression created with the New-XPathExpression cmdlet
$query1 = New-XPathQuery -AttributeName "AccountName" -Operator Equals -Value "ryan"
$query2 = New-XPathQuery -AttributeName "AccountName" -Operator Equals -Value "bob"
$queryGroup = New-XPathQueryGroup -Operator Or -Queries ($query1, $query2)
$expression = New-XPathExpression -ObjectType "Person" -QueryObject $queryGroup 

This returns an expression containing the query
/Person[((AccountName = 'ryan') or (AccountName = 'bob'))]

Building a nested query

A query group can contain other groups as well as standard queries.
$query1 = New-XPathQuery -AttributeName "DisplayName" -Operator StartsWith -Value "ryan"
$query2 = New-XPathQuery -AttributeName "DisplayName" -Operator Equals -Value "bob"
# Create the child group
$queryGroup1 = New-XPathQueryGroup -Operator Or -Queries @($query1, $query2)
$query3 = New-XPathQuery -AttributeName "Email" -Operator IsPresent
# Create the parent group using the child group and another query
$queryGroup2 = New-XPathQueryGroup -Operator And -Queries @($query3, $queryGroup1)
$expression = New-XPathExpression -ObjectType "Person" -QueryObject $queryGroup2

The expression generated is as follows
/Person[((starts-with(Email, '%')) and ((starts-with(DisplayName, 'ryan')) or (DisplayName = 'bob')))]

Last edited Sep 5, 2015 at 2:39 AM by ryannewington, version 1