I'm having issues with this powershell script below. It does work with SP 2007 but won't work with Sp 2010. My goal as you can see is to filter the list returned using the field Date - 1.

But getting the error:

You cannot call a method on a null-valued expression.

At C:\Users\AppData\Local\Temp\2\e70c0e06-6e66-4318-8480-952ccae0460e.ps1:31 char:20 + $col=$list.GetItems <<<< ($query)[0] + CategoryInfo : InvalidOperation: (GetItems:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

function Daily()

    $site    =    new-object Microsoft.SharePoint.SPSite("http://mysites/sites/Intranet/")                                                        
    #$web     =    $site.OpenWeb()                                                                                                

    $list    =    $web.Lists["Communications"]                                                                              

    #$today = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd")
    $today = (Get-Date).ToString("yyyy-MM-dd")

    $caml = '<Where>
                  <FieldRef Name="Date"/>
                  <Value Type="DateTime">
             ' -f $today 

    $query=new-object Microsoft.SharePoint.SPQuery

    $Results = @()
    foreach ($r in $col) { 

        $Title = "<b style='font-family:Calibri;'>" + $r.Title + "</b>"
        $Description = "<span style='font-family:Calibri;'>" + $r.Body + "</span>"
        $LinkToitemID = "http://Commus/DispForm.aspx?ID=" + $r.ID
        $scommsLink = "<a href=$LinkToitemID style='font-family:Calibri;'> read </a>"
        $concResult = "" + "<br><br><b style='color:#0054A4;font-size:17px;font-weight:bold'>" + $Title + "</b><br/><br/>" + $Description + "<br>" + $scommsLink + "<br><hr>"

        $Results += $concResult



    write-host $Results 

  • What's that [0] after GetItems($query)?
    – Jussi Palo
    Commented Jan 24, 2012 at 6:57

2 Answers 2


I've been fiddling around this a bit and found out that the described error occurs when list with the specified title does not exist.

Please, make sure that you have list with title "Communications" created on the http://mysites/sites/Intranet/ site collection.

If the list exist, code works fine in my environment and line $col=$list.GetItems($query)[0] doesn't raise any errors. However, I anticipate that the [0] indexer is unwanted here, because you're getting the first item of the collection to $col variable, which obviously is a logical error.

  • Omlin the [0] was an error from my end. You are right and thanks not good working too late.:( I later fond out this morning i was using $web=$site.rootWeb thinking the site was after intranet wheareas the intranet was the root site and then communications was the subsite and then a list called communications.:( One more questions though i have a field called Body and its a rich textbut i noticed it doesn't return any value.The only fields that i get values from is if its a "single line of text" Any ideas?
    – naijacoder
    Commented Jan 24, 2012 at 23:40

Your caml query may also need to be surrounded with

<query> ... <query>

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.