当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 从文件夹中获取所有文件

作者:精品下载站 日期:2024-12-14 14:35:30 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 从文件夹中获取所有文件


要求:SharePoint Online PowerShell 列出文件夹中的文件。

PowerShell 从 SharePoint Online 中的文件夹获取文件

您是否曾经需要从 SharePoint Online 中的文件夹中获取所有文件?也许您正在将文件迁移到其他位置,并且需要从文件夹中获取所有内容。也许,您想要一种快速的方法来获取给定文件夹中的所有文件,而无需浏览每个文件。无论您的原因是什么,PowerShell 都可以提供帮助!这篇博文将向您展示如何使用 PowerShell 从 SharePoint Online 中的给定文件夹获取所有文件。

在 Web 浏览器中,使用工具栏中的“导出到 Excel”按钮获取所有文件和文件夹的列表(递归地从子文件夹中获取!)。

[玩转系统] SharePoint Online:使用 PowerShell 从文件夹中获取所有文件

使用 PowerShell,我们可以从 SharePoint Online 文档库中的文件夹中检索所有文件。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Get-FilesFromFolder()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $FolderURL
    )
    Try {
        #Setup Credentials to connect
        $Cred = Get-Credential
        $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get the Folder and Files
        $Folder=$Ctx.Web.GetFolderByServerRelativeUrl($FolderURL)
        $Ctx.Load($Folder)
        $Ctx.Load($Folder.Files)
        $Ctx.ExecuteQuery()

        #Iterate through each File in the folder
        Foreach($File in $Folder.Files)
        {
            #Get Name for each File
            Write-Host $File.Name
        }
    }
    Catch {
        write-host -f Red "Error Getting Files from Folder!" $_.Exception.Message
    }
}

#Set Parameter Values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$FolderURL="/Branding/Classified/Finance Department Files"

#Call the function to get list items from folder
Get-FilesFromFolder -SiteURL $SiteURL -FolderURL $FolderURL

PnP PowerShell 从 SharePoint Online 中的文件夹获取所有文件

要递归地从文件夹中获取所有文件,我们可以使用此 PowerShell cmdlet Get-PnPFolderItem:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FolderSiteRelativeUrl = "/Shared Documents/2017" #Folder's Site Relative Path
 
#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
 
    #Get All Files from the Folder
    $FolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl -ItemType File -Recursive
    
    Write-host "Total Number of Files in the Folder:" $FolderItems.Count
    ForEach($File in $FolderItems)
    {
        $File.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

此 SharePoint Online PowerShell 获取文件夹中的所有文件。尽管上述脚本对于项目数

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯