SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 

share|improve this question
What's that [0] after GetItems($query)? – Jussi Palo Jan 24 '12 at 6:57
up vote 4 down vote accepted

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.

share|improve this answer
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 Jan 24 '12 at 23:40

Your caml query may also need to be surrounded with

<query> ... <query>

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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