How do I create multiple sub folder in one drive

PK 0 Reputation points
2023-08-17T14:34:06.6233333+00:00

Hi ,

I have multiple account folders and would like to create sub folders under each account.

For example if I have account folders named Jim, Jane, and John. I want to create a subfolder called "2023" under each one via a script is that possible in one drive?

Microsoft 365 and Office OneDrive For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AllenXu-MSFT 24,941 Reputation points Moderator
    2023-08-18T07:15:09.6733333+00:00

    Hi @PK ,

    You can use PowerShell to automate the process. Here is an example script that creates subfolder under each of the account folders named Jim, Jane, and John:

    $accounts = "Jim", "Jane", "John"
    foreach ($account in $accounts) {
        $path = "/drive/root:/$account/2023"
        $body = @{name = "2023"; folder = @{ }; "@microsoft.graph.conflictBehavior" = "rename" } | ConvertTo-Json
        $response = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/me/drive/root:$path" -Headers @{Authorization = "Bearer $accessToken"} -Method Post -Body $body -ContentType "application/json"
    }
    

    Replace $accessToken with your OneDrive API access token. This script creates a subfolder named "2023" under each account folder named Jim, Jane, and John.


    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.


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.