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

[玩转系统] SharePoint Online:使用 PowerShell 获取文档库中的文件夹

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

SharePoint Online:使用 PowerShell 获取文档库中的文件夹


要求:使用 PowerShell 获取 SharePoint Online 文档库中的文件夹。

[玩转系统] SharePoint Online:使用 PowerShell 获取文档库中的文件夹

这是我收集的一些漂亮的 PowerShell 脚本,用于在 SharePoint Online 中获取文件夹!

使用 PowerShell 通过 URL 获取 SharePoint Online 中的文件夹

您是否需要从 SharePoint Online 文档库获取特定文件夹?也许您想将该文件夹的内容复制到其他地方,或者您只是想对文件夹执行某些操作。这篇文章将向您展示如何使用 PowerShell 从文档库中获取文件夹。

下面是用于获取文档库中的文件夹的 SharePoint Online PowerShell。确保文件夹 URL 已解码!例如,“Shared%20Documents”必须解码为“Shared Documents”。


#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"

#Variables
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"
$ServerRelativeUrl= "/Sites/Marketing/Shared Documents/2017"

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

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
  
    #Get the web from URL
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.executeQuery()

    #Get the Folder object by Server Relative URL
    $Folder = $Web.GetFolderByServerRelativeUrl($ServerRelativeUrl)
    $Ctx.Load($Folder)
    $Ctx.ExecuteQuery() 
    
    #Get Some Folder Properties
    Write-host -f Green "Total Number of Files in the Folder:"$Folder.ItemCount
}
Catch {
    write-host -f Red "Error Getting Folder!" $_.Exception.Message
}

SharePoint Online PowerShell 按文件夹名称获取文档库中的文件夹

要从库中获取文件夹列表,请使用以下 PowerShell:


#Variables
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"
$LibraryName = "Documents"
$FolderName = "2017"

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

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
  
    #Get the Library
    $Library = $Ctx.web.Lists.GetByTitle($LibraryName)
    $Folders = $Library.RootFolder.Folders
    $Ctx.Load($Folders)
    $Ctx.executeQuery()

    #Get the Folder by Name
    $Folder = $Folders | Where {$_.Name -eq $FolderName}
    $Ctx.Load($Folder)
    $Ctx.ExecuteQuery() 
    
    #Get Some Folder Properties
    Write-host -f Green "Total Number of Files in the Folder:"$Folder.ItemCount
}
Catch {
    write-host -f Red "Error Getting Folder!" $_.Exception.Message
}

SharePoint Online PowerShell 列出库中的所有文件夹

您是否正在寻找一种快速简便的方法来获取 SharePoint Online 文档库中所有文件夹的列表?如果是这样,PowerShell 可以提供帮助!让我向您展示如何使用 PowerShell 获取 SharePoint Online 文档库中所有文件夹的列表。


#sharepoint online powershell list folders
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"
$ListName = "Documents"

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

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
  
    #Get the Library
    $List = $Ctx.web.Lists.GetByTitle($ListName)

    #Query to Get all Folders from the library
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>1</Value></Eq></Where></Query></View>"
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
 
    Write-host "Total Number of Folders in List:" $ListItems.count
   
    #Loop through each file in the library
    Foreach($Item in $ListItems)
    { 
        #Get the Folder Item
        $Ctx.Load($Item.Folder)
        $Ctx.ExecuteQuery()
        Write-host -f Green $Item.Folder.ServerRelativeUrl
    }
}
Catch {
    write-host -f Red "Error Getting Folder!" $_.Exception.Message
}

SharePoint Online PowerShell 遍历库中的每个文件夹:

从 PowerShell 迭代文档库中的所有文件夹怎么样?


#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 to Get Sub-folders of a Folder in SharePoint Online
Function Get-SPOFolders([Microsoft.SharePoint.Client.Folder]$Folder)
{
    Try {
        
        Write-host $Folder.ServerRelativeUrl

        #Process all Sub Folders
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        #Iterate through each sub-folder of the folder
        Foreach ($Folder in $Folder.Folders)
        {
            #Call the function recursively
            Get-SPOFolders $Folder
        }
    }
    Catch {
        write-host -f Red "Error Getting Folder!" $_.Exception.Message
    }
}

#Variables
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"
$ListName = "Documents"

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

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

#Get the Library
$List = $Ctx.web.Lists.GetByTitle($ListName)
$Ctx.Load($List.RootFolder)
$Ctx.ExecuteQuery()

#call the function to get all folders of a document library
Get-SPOFolders $List.RootFolder

用于在 SharePoint Online 中获取文件夹的 PnP PowerShell

这是用于获取 SharePoint 文档库中的文件夹的 PnP PowerShell!确保在运行此脚本之前安装了 PnP PowerShell 模块。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ParentFolderURL= "/Shared Documents" #Parent Folder's Site Relative Path
$FolderName="2017"

#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Connect to SharePoint Online site
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
 
    #Get the Folder
    $Folder = Get-PnPFolderItem -FolderSiteRelativeUrl $ParentFolderURL -ItemName $FolderName
    
    Write-host "Total Number of Items in the Folder:" $Folder.ItemCount
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

我们还可以使用 Get-PnPFolder cmdlet 获取文件夹。这是一个例子:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FolderURL = "/Shared Documents/2018"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the SharePoint library Folder
$Folder = Get-PnPFolder -Url $FolderURL

递归获取文档库中的所有文件夹

要以递归方式从文档库中获取所有文件夹和子文件夹,请使用:


#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/2018"
$FolderURL = "/Shared Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive  #-Credentials (Get-Credential)

#Get all Sub-Folders of folder
Function Get-SubFolders($FolderURL)
{
    #Get all sub-folders of the Folder
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder | Where {$_.Name -ne "Forms" -and $_.Name -ne "Document"}

    #Loop through the folders
    ForEach($SubFolder in $SubFolders)
    {
        $SubFolderURL = $FolderUrl+"/"+$SubFolder.Name
        write-host -ForegroundColor Green $SubFolder.Name " - " $SubFolderURL
              
        #Call the function recursively
        Get-SubFolders $SubFolderURL
    }
}
  
#Call the function with the root folder of the doc library
Get-SubFolders $FolderURL

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

要从文件夹中获取所有文件夹,请使用以下 PowerShell:


#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/Marketing"
$FolderSiteRelativeURL = "/Branding/2020"

#Connect to the Site collection
Connect-PnPOnline -URL $SiteURL -Interactive

#Get the Folder from site relative URL
$Folder = Get-PnPFolder -Url $FolderSiteRelativeURL

#Get all Subfolders of a folder - recursively
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder -Recursive

#Get Server relative URL of all subfolders
$SubFolders | Select ServerRelativeURL

从给定文件夹中递归获取所有子文件夹怎么样?


$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder -Recursive

使用 PnP PowerShell 从文件夹中获取所有项目:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FolderURL= "/Shared Documents/2018" #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 Items inside the Folder
    $FolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL
    
    Write-host "Total Number of Items in the Folder:" $FolderItems.Count
    ForEach($Item in $FolderItems)
    {
        Write-host $Item.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

以下是计算 SharePoint Online 文档库中每个文件夹上的文件和子文件夹数量的示例:使用 PowerShell 获取 SharePoint Online 文档库中每个文件夹上的文件和子文件夹计数

经常问的问题:

如何使用 PowerShell 在 SharePoint Online 中创建文件夹?

在 SharePoint Online 中创建文件夹是组织文档和其他内容的快速而简单的方法。若要使用 PowerShell 创建文件夹,请连接到 SharePoint Online,然后运行 PnP PowerShell 中的 Add-PnPFolder cmdlet 或运行 CSOM 中的Folders.Add 方法。
详细信息:在 SharePoint Online 中创建文件夹

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

取消回复欢迎 发表评论:

关灯