0

A basic question, yet I'm stuck (I'm still a beginner):

I currently have a SharePoint List with two printers in it, and I have the following code:

$l = Get-PnPList -Identity "TellerstandPrinters"
$items = Get-PnPListItem -List $l
foreach ($item in $items) {
Get-PnPListItem -list $l }

This prints both printers to the console twice. Output:

Id    Title                                              GUID
--    -----                                              ----
1     Test                                               05899126-d4fb-41df-ad88-e57b4a4848e2
2     Test2                                              4d8c8cab-6770-4d0f-9b71-165602593f9e
1     Test                                               05899126-d4fb-41df-ad88-e57b4a4848e2
2     Test2                                              4d8c8cab-6770-4d0f-9b71-165602593f9e

Now, I think I can see why: There are two items in the list items which makes the loop print it twice. Do I have to use some form of array to prevent this from happening? I'm trying to find a clear answer on this site and Google but I'm still a newb and can't figure it out.

Thanks!

  • You have 2 items in the list. You are calling the list to get all the list items, and then loop that against the number of items in the list. Add another item to the list and your output will be the 3 listitems, 3 times. What exactly are you trying to do? – Christoffer Apr 3 '18 at 11:48
  • You can get the same results using a one-liner. Get-PnPListItem -List TellerstandPrinters – Christoffer Apr 3 '18 at 11:52
0

It seems the for loop is not necessary at all, I'm silly. For reference,

Get-PnPListItem -list $l 

is enough, as pointed out by Christoffer. Thanks for the comments. Answered.

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.