Scripts/SharePoint.ps1



#region Remove Libraries

$csv = @"
"Title", "Id"
"1001 - Antenna Uitzendburo VOF", "4d8ba3c9-a9e7-417b-aced-7b66180d0389"
"1002 - Antenna Uitzend Buro BV", "bd4af130-6327-45b2-95f0-6709f36f112f"
"1003 - AUB Beheer B.V", "08302d3a-fb0a-4abb-9462-52f784252e1e"
"@




$listsToRemove = $csv | ConvertFrom-Csv

$listsToRemove | ForEach-Object {

    Remove-PnPList -Identity $_.Id -Force -Verbose

}



#endregion




#region Create folders in Library


#Config Variables
# $SiteURL = "https://crescent.sharepoint.com/sites/Ops"
$csv = @"
"Title", "Id"
"1001 - Antenna Uitzendburo VOF", "4d8ba3c9-a9e7-417b-aced-7b66180d0389"
"1002 - Antenna Uitzend Buro BV", "bd4af130-6327-45b2-95f0-6709f36f112f"
"1003 - AUB Beheer B.V", "08302d3a-fb0a-4abb-9462-52f784252e1e"
"@


# $csv = @"
# "Title", "Id"
# "@


$LibraryName = "Klanten"

Try {
    #Connect to PnP Online
    # Connect-PnPOnline -Url $SiteURL -Interactive
    $Web = Get-PnPWeb

    #Get the Document Library and its site relative URL
    $Library = Get-PnPList -Identity $LibraryName -Includes RootFolder
    If ($Web.ServerRelativeUrl -eq "/") {
        $LibrarySiteRelativeURL = $Library.RootFolder.ServerRelativeUrl
    } else {
        $LibrarySiteRelativeURL = $Library.RootFolder.ServerRelativeUrl.Replace($Web.ServerRelativeUrl, '')
    }

    #Get the CSV file
    $CSVFile = $csv | ConvertFrom-Csv

    #Read CSV file and create folders
    ForEach ($Row in $CSVFile) {
        #Replace Invalid Characters from Folder Name, If any
        $FolderName = $Row.Title
        $FolderName = [RegEx]::Replace($FolderName, "[{0}]" -f ([RegEx]::Escape([String]'\"*:<>?/\|')), '_')

        $FolderName

        #Frame the Folder Name
        $FolderURL = $LibrarySiteRelativeURL + "/" + $FolderName

        #Create Folder if it doesn't exist
        Resolve-PnPFolder -SiteRelativePath $FolderURL #| Out-Null
        Write-Host "Ensured Folder:"$FolderName -f Green
    }
} catch {
    Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}


#endregion






Connect-PnPOnline -Url "https://klant-admin.sharepoint.com/" -UseWebLogin


$sites = Get-PnPTenantSite

$sites = $sites | Where-Object HubSiteId -EQ "fe9fe2b2-43f4-40cd-96b7-61ae270fa80c"

foreach ($site in $sites) {
    Write-Host "Processing site: $($site.Url)"

    Connect-PnPOnline -Url $site.Url -UseWebLogin

    # Get all libraries in the site
    $libraries = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 } # Document Libraries only

    foreach ($library in $libraries) {
        Write-Host "Setting 'Open in Desktop App' for library: $($library.Title) in site: $($site.Url)"

        # Set the default opening behavior
        Set-PnPList -Identity $library.Id -OpenDocumentsMode ClientApplication
    }
}