SharePoint Online: Get Email, Display Name, Login ID from “Created By” Column in PowerShell
Requirement: Get User Name, Email, Login ID, etc., from the “Created By” field value of a List Item using PowerShell.
PowerShell to Retrieve User Object from “Created By” Field:
Have you ever wanted to obtain the Email of a user who created an item in SharePoint Online in the middle of a PowerShell script?
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ItemID = "1"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List Item
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ItemId)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Get the "Created By" column value of the Item
$CreatedBy = $ListItem.FieldValues["Author"]
#Get the User Object
$User = $Ctx.Web.GetUserById($CreatedBy.LookupId)
$Ctx.Load($User)
$Ctx.ExecuteQuery()
$User | Select Email, Title, LoginName
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
PnP PowerShell to Get Emails of Users who have Created the Documents
How about getting emails from the uploaded documents? Let’s retrieve users’ emails who uploaded files in a SharePoint Online document library.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Branding"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Files from the document library - In batches of 500
$Files = Get-PnPListItem -List $ListName -PageSize 500 | Where {$_.FileSystemObjectType -eq "File"}
#Loop through all documents
$DocumentsData=@()
ForEach($File in $Files)
{
#Collect Documents Data
$DocumentsData += New-Object PSObject -Property @{
FileName = $File.FieldValues.FileLeafRef
URL = $File.FieldValues.FileRef
AuthorEmail = $File.FieldValues.Author.Email
}
}
#Get Files Data
$DocumentsData