0

What is the correct powershell syntax for finding all list items in a given list where a Person field matches a user retrieved from Get-SPUser?

I.e.

$user = Get-SPUser -web *web* -Identity *domain\user*

$listItems = $web.Lists["*listName*"].Items | Where-Object $_.["*personField*"] -eq $user

1 Answer 1

1

Here's something I wrote for another project:

Function Convert-SPFieldUserValueToSPUser {
    param (
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [Microsoft.SharePoint.SPListItem] $item,
        [Alias("field","name")]
        [string] $fieldName
    )

    ($item.Fields[$fieldName]).GetFieldValue($item[$fieldName]).User | Write-Output
}

Then, you can use this function as follows:

$userLogin = '*domain\user*';
$listItems = $web.Lists["*listName*"].Items | `
    Where-Object (($_ | Convert-SPFieldUserValueToSPUser -field "*personField*").LoginName -eq $userLogin);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.