> For the complete documentation index, see [llms.txt](https://docs.lithnet.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lithnet.io/resource-management-powershell/v1.0/usage/cmdlet-reference/new-xpathquery.md).

# New-XPathQuery

## Description

The `New-XPathQuery` cmdlet create a new query object that can be used to create an XPath expression using \[New-XPathExpression], or an element in an XPath query group using [New-XPathQueryGroup](/resource-management-powershell/v1.0/usage/cmdlet-reference/new-xpathquerygroup.md). The resulting [XPathExpression](/resource-management-powershell/v1.0/usage/cmdlet-reference/new-xpathexpression.md) can be passed as a parameter to [Search-Resources](/resource-management-powershell/v1.0/usage/cmdlet-reference/search-resources.md). An XPath query object contains the component of an expression that compares an attribute with a value.

For a list of examples filters generated by the XPath cmdlets, see the [XPath expression examples](/resource-management-powershell/v1.0/help-and-support/xpath-expression-examples.md) page.

## Syntax

```
New-XpathQuery [-AttributeName] <string> [-Operator] <ComparisonOperator> {Equals | NotEquals | GreaterThan | GreaterThanOrEquals | LessThan | LessThanOrEquals | IsPresent | IsNotPresent | Contains | StartsWith | EndsWith} [[-Value] <Object>] [[-Negate]]
```

## Parameters

### AttributeName

Required. The name of the attribute to compare

### Operator

Required. The operator to apply to the attribute

### Value

Optional if using the IsPresent or IsNotPresentOperator. Required for all other operators. The value to use in the comparison. This can be any value that can be converted to the data type of the attribute being searched. Reference attributes can also specify another [XPath expression](/resource-management-powershell/v1.0/usage/cmdlet-reference/new-xpathexpression.md) as the search value. See the dereferencing example below.

### Negate

Optional. Negates the expression by inverting the comparison operation. For example, using the `Negate` switch, the LessThan operator becomes *not LessThan*. Note that the [FIM XPath dialect](https://technet.microsoft.com/en-us/library/ff356871\(WS.10\).aspx) does not support double negation. Therefore using the `Negate` parameter on IsNotPresent and NotEquals operators is not supported.

## Examples

### Viewing the query string

The `New-XPathQuery` 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.

```powershell
$query = New-XPathQuery -AttributeName "AccountName" -Operator Equals -Value "ryan"
$query.ToString()
```

This returns an string representing the query component

```
(AccountName = 'ryan')
```

### Building a simple query

In order to use the query, it must be rendered by an XPath expression created with the \[New-XPathExpression] cmdlet

```powershell
$query = New-XPathQuery -AttributeName "AccountName" -Operator Equals -Value "ryan"
$expression = New-XPathExpression -ObjectType "Person" -QueryObject $query
$expression.ToString()
```

This returns an expression containing the query

```
/Person[(AccountName = 'ryan')]
```

You can also use the pipeline to pass the query to the \[New-XPathExpression] cmdlet.

```powershell
$expression =  New-XPathQuery -AttributeName "AccountName" -Operator Equals -Value "ryan" | New-XPathExpression -ObjectType "Person" 
$expression.ToString()
```

### Testing for presence

The IsPresent and IsNotPresent operators work without the `Value` parameter

```powershell
$query = New-XPathQuery -AttributeName "AccountName" -Operator IsPresent
$expression = New-XPathExpression -ObjectType "Person" -QueryObject $query
$expression.ToString()
```

This returns an expression containing the query

```
/Person[(starts-with(AccountName, '%'))]
```

### Using another XPath expression to query a reference attribute

```powershell
# Build an expression to find a user by its account name
$query = New-XpathQuery -AttributeName "AccountName" -Operator Equals -Value "ryan"
$expression = New-XpathExpression -ObjectType "Person" -Query $query

# Build a query to find all users with a manager who has the account name 'ryan'
$derefQuery = New-XpathQuery -AttributeName "Manager" -Operator Equals -Value $expression
$derefExpression = New-XpathExpression Person $derefQuery
$derefExpression.ToString()
```

This returns an expression containing the query

```
/Person[(Manager = /Person[(AccountName = 'ryan')])]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.lithnet.io/resource-management-powershell/v1.0/usage/cmdlet-reference/new-xpathquery.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
