SharePoint Online: Get File Properties using PowerShell

Requirement:  Get File Properties in SharePoint Online using PowerShell
 
SharePoint Online PowerShell to Get File Properties
#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"

Function Get-SPOFileProperties($SiteURL,$FileRelativeURL)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get the File
        $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
        $Ctx.Load($File)
        $Ctx.Load($File.Author)
        $Ctx.Load($File.ModifiedBy)        
        $Ctx.Load($File.CheckedOutByUser)
        $Ctx.Load($File.Author)
        $Ctx.ExecuteQuery()

        #Get File Properties
        Write-host "File Name:" $File.Name
        Write-host "Title:" $File.Title
        Write-host "File Size:" $File.Length
        Write-host "Created:" $File.TimeCreated
        Write-host "Last Modified:" $File.TimeLastModified
        Write-host "Created By:" $File.Author.Title
        Write-host "Modified By:" $File.ModifiedBy.Title
        Write-host "Checked Out To:" $File.CheckedOutByUser.Title
        Write-host "UI Version:" $File.UIVersion
        Write-host "Major Version:" $File.MajorVersion
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Call the Function with Site URL and File URL
Get-SPOFileProperties -SiteURL "https://crescenttech.sharepoint.com" -FileRelativeURL "/Shared Documents/Discloser Asia.docx"

PowerShell to Get Document Metadata in SharePoint Online:
If you need to retrieve custom column values of a file:
#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"

#Set Variables
$SiteURL = "https://crescenttech.sharepoint.com" 
$FileRelativeURL = "/Docs/Compliance Process.xlsx"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the File
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
    $Ctx.Load($File)

    #Get the List Item with all metadata fields of the File
    $ListItem = $File.ListItemAllFields
    $Ctx.Load($ListItem)
    $Ctx.ExecuteQuery()

    #Get Metadata of the File
    Write-host "File Name:" $File.Name
    
    #Get the "Status" column value of the File
    Write-host "Status:" $ListItem["Status"]
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
SharePoint Online: Get File Properties using PowerShell SharePoint Online: Get File Properties using PowerShell Reviewed by Salaudeen Rajack on March 05, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.