This sample PowerShell script demonstrate how to get SharePoint List Items/fieds via PowerShell using the Microsoft Graph API via REST.
This sample requires MSAL.NET library for obtaining an Access Token used to query the Microsoft Graph API. Because PowerShell does not have direct integration with nuget, follow the instructions for manual downloading MSAL.NET:
- Go to https://www.nuget.org/packages/Microsoft.Identity.Client and select 'Manual Download' to download the MSAL
nupkg
file. - Rename the file extension to .zip
- Extract the zip file to
{path}\packages\Microsoft.Identity.Client.1.1.0-preview
- where{path}
is the folder where your script is saved
You need to register an application to be able to access the Microsoft Graph API. In order to do this:
- Go to https://apps.dev.microsoft.com/portal/register-app
- Add a name for the application and make sure the Guided Setup option is unchecked
- Click
Create
- Now configure the new application to be a
Native App
by clickingAdd Platforms
, selectingNative Application
and then clickSave
- Copy the Guid under
Application Id
to the clipboard
- Open your PowerShell script and replace the
YourAppIdHere
with the Application Id for your application you just registered - Now add your Site Relative URL and the List Name by replacing the values of
YourSiteRelativeUrlHere
andYourListNameHere
##Please replace the three values below
$appId = "YourAppIdHere"
$SiteRelativeUrl = "YourSiteRelativeUrlHere"
$ListName = "YourListNameHere"
Open your SharePoint List in a browser, then check the image below to find out the values for Site Relative URL and your List Name. For the list name, can use both URL encoded with %20 or spaces:
Start a PowerShell session then run the script:
.\PS-SharePoint-List-Graph.ps1