Following one-liner failed

Get-PnPGroup | where-Object {$_.title -eq 'test account'} | Set-PnPListItemPermission -Identity 1 -List "Accounts" -AddRole "Read" -Group $_.id

yielding error

Set-PnPListItemPermission : Cannot bind argument to parameter 'Group' because it is null.
At line:1 char:139
+ ...  "Read" -Group $_.id
+                    ~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-PnPListItemPermission], ParameterBindingV 
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,SharePointPnP.Po 

However, by breaking the pipe into 2 commands it works

$group = Get-PnPGroup | where-Object {$_.title -eq 'test account'} 
Set-PnPListItemPermission -Identity 1 -List "Accounts" -AddRole "Read" -Group $group.id 

How to run the command in one pipeline?


You're missing a foreach-object around Set-PnPListItemPermission

Get-PnPGroup | where-Object {$_.title -eq 'test account'} | Foreach-Object {Set-PnPListItemPermission -Identity 1 -List "Accounts" -AddRole "Read" -Group $_.id}

But also be aware that the only thing you gain by putting it on one line is:

  • Harder to debug
  • Slower execution
  • Not potentially overwriting variable
  • That works. One liner is preferred because in my case the command string is composed from Excel rows and bulk executed. I know I can also write a full-blown ps1 script to parse Excel rows to list and iterate but it's a matter of perception of convenience. – abbr Feb 9 '18 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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