Who's Online
2 visitors online now
0 guests, 2 bots, 0 members
Support my Sponsor
  • An error has occurred, which probably means the feed is down. Try again later.

Archive for March, 2023

SharePoint PowerShell to identify all OneDrive sites and migrate content to SharePoint sites

I wanted to perform migration of few of my users content to their respective OneDrive site. To do so, I used below PowerShell to identify URL of all users who already have a OneDrive sites and then used FileMigration wizard to import their content in the respective OneDrive Site.

$TenantUrl = Read-Host "https://tenantname-admin.sharepoint.com/"
$LogFile = [Environment]::GetFolderPath("Desktop") + "\OneDriveSites.log"
Connect-SPOService -Url $TenantUrl
Get-SPOSite -IncludePersonalSite $true -Limit all -Filter "Url -like '-my.sharepoint.com/personal/'" | Select -ExpandProperty Url | Out-File $LogFile -Force
Write-Host "Done! File saved as $($LogFile)."
Once you have the list of OneDrive path, go to SharePoint Admin center > Select Migration > File Share Migration > Download and install the migration agent.
Note : Note agent should be installed on a system which should have access to the files through network drive we want to migrate
Use the user which you wish to use for migration and then provide the FileSharePath at the bottom of the screen. You can test if you have access to the UNC path using test button.
Once everything is setup, you need can close this agent and let it run in the background. It will take sometime for the agent to appear in the SharePoint Admin center Agents screen

Select Add task from Migration screen
Select ‘Single source and destination’ and then select next
Put the path of the users folder and select next
Select OneDrive icon and select next
Select the OneDrive path URL and then select the Document library you want the files to be migrated. Then select Next
Prove the TaskName, Select the Active Agents and make sure you select Perform scan only and Preserve file share permissions as required. Select Next

PowerShell to find duplicate files on SharePoint online site

You can use below PowerShell to find all duplicate files(regardless of document libraries) on SharePoint online site.

Make sure you update the parameters such as SiteURL and ReportOutput. This may take time based on number of items in SharePoint site. I have kept it running for 3 days and it worked like a charm. I have seen other Powershell’s which don’t work if you have MFA enabled whereas this script works regardless you have MFA enabled or disabled.

#Before running the below script, kindly follow below steps :
#1. Open you PowerShell ISE in your system and run it as administrator
#cls2. Install the New PnP PowerShell Module using below commands:
        Install-Module PnP.PowerShell
#3. Register a new Azure AD Application and Grant Access to the tenant
#Then paste and run below pnp script:

$SiteURL = "https://tenantname.sharepoint.com/sites/Sitename"
$Pagesize = 2000
$ReportOutput = "C:\Temp\Sitename.csv"
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
#Array to store results
$DataCollection = @()
#Get all Document libraries
$DocumentLibraries = Get-PnPList | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $_.ItemCount -gt 0 -and $_.Title -Notin("Site Pages","Style Library", "Preservation Hold Library")}
#Iterate through each document library
ForEach($Library in $DocumentLibraries)
    #Get All documents from the library
    $global:counter = 0;
    $Documents = Get-PnPListItem -List $Library -PageSize $Pagesize -Fields ID, File_x0020_Type -ScriptBlock `
        { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($Library.ItemCount) * 100) -Activity `
             "Getting Documents from Library '$($Library.Title)'" -Status "Getting Documents data $global:Counter of $($Library.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
    $ItemCounter = 0
    #Iterate through each document
    Foreach($Document in $Documents)
        #Get the File from Item
        $File = Get-PnPProperty -ClientObject $Document -Property File
        #Get The File Hash
        $Bytes = $File.OpenBinaryStream()
        $MD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
        $HashCode = [System.BitConverter]::ToString($MD5.ComputeHash($Bytes.Value))
        #Collect data       
        $Data = New-Object PSObject
        $Data | Add-Member -MemberType NoteProperty -name "FileName" -value $File.Name
        $Data | Add-Member -MemberType NoteProperty -Name "HashCode" -value $HashCode
        $Data | Add-Member -MemberType NoteProperty -Name "URL" -value $File.ServerRelativeUrl
        $Data | Add-Member -MemberType NoteProperty -Name "FileSize" -value $File.Length       
        $DataCollection += $Data
        Write-Progress -PercentComplete ($ItemCounter / ($Library.ItemCount) * 100) -Activity "Collecting data from Documents $ItemCounter of $($Library.ItemCount) from $($Library.Title)" `
                     -Status "Reading Data from Document '$($Document['FileLeafRef']) at '$($Document['FileRef'])"
#Get Duplicate Files by Grouping Hash code
$Duplicates = $DataCollection | Group-Object -Property HashCode | Where {$_.Count -gt 1}  | Select -ExpandProperty Group
Write-host "Duplicate Files Based on File Hashcode:"
$Duplicates | Format-table -AutoSize
#Export the duplicates results to CSV
$Duplicates | Export-Csv -Path $ReportOutput -NoTypeInformation