powershell how to use Add-PnPField -type user to set to users only

Chris Mancey (Group) 221 Reputation points
2020-07-30T13:02:35.803+00:00

I'm reasonably new to PowerShell and have created a script to deploy a list into SharePoint using PNP. One of the list items is a user field that is created with the following line

Add-PnPField -List "Owners" -DisplayName "Owner" -InternalName Owner -Type User -Required -ErrorAction Continue -AddToDefaultView | Out-Null

Can anyone tell me how I set this field to be users only and not users & groups?

SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,832 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Trevor Seward 11,691 Reputation points
    2020-07-31T03:26:36.88+00:00

    Use Add-PnPFieldFromXml instead.

    Try something like:

    $xml = '<Field Type="User" DisplayName="Owner" List="Owners" Required="FALSE" ID="{<guid>}" -Required UserSelectionMode="PeopleOnly" StaticName="Owners" Name="Owners" Description=""/>'
    Add-PnPFieldFromXml -FieldXml $xml
    
    0 comments No comments

  2. Emily Du-MSFT 42,346 Reputation points Microsoft Vendor
    2020-07-31T09:20:33.75+00:00

    You could try below PowerShell.

    $field = Get-PnPField -List "Owners" -Identity "Owner"
    $field = Add-PnPField -List "Owners" -DisplayName "Owner" -InternalName Owner -Type User -Required -ErrorAction Continue -AddToDefaultView | Out-Null
    Set-PnPField -List "Owners" -Identity $field.Id -Values @{"SelectionMode"=0}
    
    0 comments No comments