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.