1

I'm just getting started with Powershell and am trying to write a reusable script that lets me get all items from an arbitrary list:

function Get-SPItems
{
<# 
    .SYNOPSIS  
        Get all Items from a Sharepoint List
    .PARAMETER list
        Which List to get items from
    .PARAMETER source
        Optional Sharepoint Source
    .EXAMPLE 
     Send-WOL -list Hosts
#>

    param(
        [string]$list,
        [string]$source = "https://mysite.com"
    )

    # grab snapin
    if ((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
        Add-PSSnapin Microsoft.SharePoint.PowerShell;
    }

    $spSourceWeb = Get-SPWeb $source
    $list = $spSourceWeb.Lists[$list]

    return $list.GetItems()

}

I want to be able to call this function from any other script, such as:

. .\Get-SPList.ps1

$items = Get-SPList -list "MyList"

foreach ($item in $item) {
    Write-Host $item["ID"]
}

This is not working how I want it, unfortunately execution of the below script yields this error:

Get-SPList : The term 'Get-SPList' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Both files are in the same directory, which is also the working directory.

2 Answers 2

5

Your method is not called Get-SPList but Get-SPItems

So,

$items = Get-SPItems -list "MyList"

foreach ($item in $item) {
    Write-Host $item["ID"]
}
1
  • I think you missed to attach the error? :) Mar 7, 2014 at 8:51
2

Make sure you are using PowerShell ISE 64bit.

Below code should be executed before your script:-

#Check and add SharePoint PowerShell snap-in
if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

It basically is adding the snapping which contains the methods like Get-SPWeb..

2
  • Get-SPItems is his own custom function, and thus not loaded through the SnapIn. He does need it for Get-SPWeb and such, though. Mar 7, 2014 at 9:15
  • corrected.. I was suggesting in general.
    – variable
    Mar 7, 2014 at 9:46

Your Answer

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

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