2

I have a short script which is going to be part of a larger one but I can't get it to work. It works on every other list I've tried running it against but not this one in particular. The $myList.ItemCount returns the correct number of list items as 4987 but the $items.Count returns 0 and the foreach loop returns no results. Anyone ever deal with this issue? I'm sure it has something to do with the list itself as I made a template of it, copied it to another site, ran the exact same script with the updated url and it worked.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue 

$myWeb = Get-SPWeb "https://mysiteurl.org"
$myList = $myWeb.Lists["My List"]
$myView = $myList.Views["All Items"]
$items = $myList.GetItems($myView)

Write-Output $myList.ItemCount
write-output $items.Count

foreach($item in $items)
{
Write-Output $item
}

Any help would be greatly appreciated.

2 Answers 2

1

You can try without using any view

$SPWeb = Get-SPWeb "https://mysiteurl.org"
$SPList = $SPWeb.Lists["My List"]
$SPListItems = $SPList.Items #or $SPList.GetItems()
$SPListItems.Count
$SPWeb.Dispose()
0

I tested the script and it is working fine, i think you have to check couple of things:

  • Make sure it is list not a library, if it is not a list then view will be different. i.e. for document library it will be All Documents Run the below script to get the all views from the list.

$myWeb = Get-SPWeb "https://mysiteurl.org"

$myList = $myWeb.Lists["My List"]

$myView = $myList.Views

$myView.views.Title

Now review the output and you will see if All items or any other All type of view available.

Your Answer

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

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