Bulk create folder in OneDrive accounts using powershell via csv

DWTK 61 Reputation points
2022-05-17T18:53:24.667+00:00

Hello,

I'm wondering if it's possible to use powershell to create a folder (same name) in all OneDrive accounts in a tenant. I've searched around and haven't seen anything that specific. Any help would be great.

Thank you,

Microsoft 365 and Office SharePoint For business Windows
Microsoft 365 and Office OneDrive For business Windows
{count} votes

2 answers

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,596 Reputation points Microsoft External Staff
    2022-05-20T08:10:49.853+00:00

    Hi @DWTK ,

    Your requirement can be achieved via PnPPowershell.
    It is important to note that user’s OneDrive account is private by default. Even Global Office 365 administrators do not have access to other users’ OneDrive. We need to grant the user admin access to all OneDrive sites before creating folder in these sites.

    Please follow the steps:

    1,Grant Admin Access to All OneDrive for Business

    #Set Parameters  
    $AdminSiteURL="https://tenant-admin.sharepoint.com"  
    $SiteCollAdmin="******@tenant.onmicrosoft.com"  
    #Connect to PnP Online to the Tenant Admin Site  
    Connect-PnPOnline -Url $AdminSiteURL -Interactive  
        
    #Get All OneDrive Sites  
    $OneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"  
      
    #Loop through each site  
    ForEach($Site in $OneDriveSites)  
    {    
        #Add Site collection Admin  
        Set-PnPTenantSite -Url $Site.URL -Owners $SiteCollAdmin  
    Write-Host -f Green "Added Site Collection Admin to: "$Site.URL  
    }  
    

    203988-1.png

    2, Then, please run the below PowerShell script to create folder to all OneDrive as an admin.

    #Parameters  
    $AdminCenterURL = "https://tenant-admin.sharepoint.com"  
    $FolderName = "Archives"  
       
    #Get Credentials to connect  
    $Cred = Get-Credential  
       
    Try {  
        #Connect to Admin Center  
        Connect-PnPOnline -Url $AdminCenterURL -Credential $Cred  
       
        #Get All OneDrive sites  
        $OneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"  
       
        #Iterate through Each OneDrive  
        ForEach($Site in $OneDriveSites)  
        {    
            Try {  
                Write-host -f Yellow "Ensuring Folder '$FolderName' in $($Site.URL)" -NoNewline  
                #Connect to OneDrive site  
                $SiteConn = Connect-PnPOnline -Url $Site.URL -Credential $Cred -ReturnConnection -ErrorAction Stop  
       
                #ensure folder in SharePoint Online using powershell  
                $NewFolder = Resolve-PnPFolder -SiteRelativePath "Documents/$FolderName" -Connection $SiteConn -ErrorAction Stop  
       
                Disconnect-PnPOnline -Connection $SiteConn  
                Write-host -f Green " Done!"  
            }  
            Catch {  
                write-host "`tError: $($_.Exception.Message)" -foregroundcolor Red  
            }  
        }  
    }  
    Catch {  
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red  
    }  
    

    204450-2.png
    204005-3.png

    3, Remove user admin access from all OneDrive sites.

    #Set Parameters  
    $AdminSiteURL="https://tenant-admin.sharepoint.com"  
    $SiteCollAdmin="******@tenant.onmicrosoft.com"  
    #Connect to PnP Online to the Tenant Admin Site  
    Connect-PnPOnline -Url $AdminSiteURL -Interactive  
        
    #Get All OneDrive Sites  
    $OneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"  
      
    #Loop through each site  
    ForEach($Site in $OneDriveSites)  
    {    
        Connect-PnPOnline $_.Url -Credentials $Cred  
        $User = Get-PnPUser | Where { $_.LoginName -like $LoginID}  
        If($User -ne $Null)  
        {  
            Remove-PnPSiteCollectionAdmin -Owners $LoginID  
            Write-Host "`tRemoved user from Site Collection Administrator Group!" -f Green  
        }  
    

    Thanks,
    Yanli Jiang

    ===========================================

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



    Updated on 2022.05.30
    This user account should be a global admin, otherwise it does not have permission to enter the SharePoint admin center, and therefore has no permission to grant permission. If the account does not have this permission, you can ask your administrator to assign this permission to you.

    $SiteCollAdmin="******@tenant.onmicrosoft.com"  
    

    Thanks,
    Yanli Jiang

    ===========================================

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



    Updated on 2022.05.31
    The "-Interactive" switch is required if connecting to PnPOnline with an MFA-enabled account. This part of the script in the answer contains the "-Interactive" switch.

       #Connect to PnP Online to the Tenant Admin Site  
        Connect-PnPOnline -Url $AdminSiteURL -Interactive  
    

    The reason for your previous problem is the account's permission, not related to MFA.

    Thanks,
    Yanli Jiang

    ===========================================

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



    Updated on 2022.06.02
    This may be related to your account is not global admin. I used an account that is not a global admin to test, and the same error as you appeared.
    207680-06026.png
    207748-06024.png

    You can check whether the account you are using is a global admin.
    207767-06023.png

    Go to Microsoft 365 admin center
    You can see that the account I used in the previous test was the global admin account.
    207841-06027.png
    You can edit the user's role by clicking "Manage roles".

    Thanks,
    Yanli Jiang

    ===========================================

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Vasil Michev 119.5K Reputation points MVP Volunteer Moderator
    2022-05-18T06:47:52.887+00:00

    You will need to use the PnP module, or call CSOM directly. Here's the Add-PnPFolder cmdlet help: https://pnp.github.io/powershell/cmdlets/Add-PnPFolder.html
    Alternatively, you can use the Graph API: https://learn.microsoft.com/en-us/graph/api/driveitem-post-children?view=graph-rest-1.0&tabs=http


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.