Using powershell to create folders within users onedrive

Bronze Contributor

Hi all,

I'm experiencing several issues with different PowerShell versions when trying to create folders in OneDrive for users in bulk.


PowerShell 5.1 does not recognize Connect-PnPOnline.
PowerShell 7 does not recognize Connect-SPOService.

I have been following the instructions from this guide, which worked on my previous device. However, I’m unable to get it to work on my new device.


My goal is to create folders within specific users' OneDrive accounts. Could you please assist me in resolving this?

Thank you!

24 Replies

Hi @Harm_Veenstra 


Thanks but the link you provided shows how to create folders within a specifc sharepoint list, my end goal is to create folders within multiple users onedrives. 


Sorry, my bad... Wrong link... But something like this, I guess,, should work. The most important is to install the pnp.powershell module. You can use that in PS7 too, but I remember that in the past I had to use Import-Module -Name pnp.powershell -UseWindowsPowerShell

I have used this script to grant permissions before, but I now need to restrict it to only add permissions to a specific set of OneDrives instead of all OneDrives. I would like to modify the script so that it reads a CSV file to determine which users should have their OneDrive permissions updated.

When the script reaches the folder creation section, I want it to reference the URLs for the OneDrives I granted access to earlier, and create folders only for the users listed in the same CSV file.

How can I adjust the code to achieve this?

Hi @Harm_Veenstra 


Any suggestions? 


Thanks in advance

You can check before creating directories if the user is in the CSV. What does the CSV look like, and what is your script so far?

Hi @Harm_Veenstra 


So I have a list of users I want to retrive the onedrive URLs for and the title is UserPrincipalName (I have also tried "Email"

The code I am using is

$myUsers = Import-Csv -Path 'C:\Users\Salamander\Downloads\UsersToPreProv.csv'
New-PnPPersonalSite -Email $myUsers




Screenshot 2024-07-10 at 12.35.39.png


Any ideas? 


Should be:
$myUsers = Import-Csv -Path 'C:\Users\Salamander\Downloads\UsersToPreProv.csv'
foreach ($user in $myUsers) {
New-PnPPersonalSite -Email $user

But what does your csv file look like? When you run this, what do you see and what does $user contain after running?

Hi @Harm_Veenstra 


So when I run the code you said I get this: 


Screenshot 2024-07-10 at 13.26.59.png


Here is my CSV: 


Screenshot 2024-07-10 at 13.27.14.png

$myUsers = Import-Csv -Path 'C:\Users\Salamander\Downloads\UsersToPreProv.csv'
foreach ($user in $myUsers) {
New-PnPPersonalSite -Email $user.UserPrincipalName

But did you Connect-PnPOnline first?

Hi @Harm_Veenstra 


Yes, I used it first: 


Screenshot 2024-07-10 at 14.22.25.png

And did you grant yourself permission first to that url of the personal folder?
No as I have tried to follow this:

The steps in the link, permissions are done after the URLs are exported.

New-PnPPersonalSite creates the site, but the site is already there. You can follow the steps in the article to achieve what you want?

The steps no longer work for me, this is the reason I put this post on here, I was following the guide before. No luck.


The link you posted gives me access to all urls but I dont want that I want to export URLs for several users then give myself access to them, to create the folders. 

If you skip the creation part, but have a csv file with a UserPrincipalName header and all the users email addresses in that column below that... Then you can run this to get the urls

#Import users
$myUsers = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\UsersToPreProv.csv"

#Retrieve the URLs + export them
$ODFBurls = @()
foreach ($user in $myUsers) {
$ODFBurls += Get-PnPUserProfileProperty -Account $user.UserPrincipalName
$ODFBurls | Select-Object PersonalUrl | Export-Csv -Path "C:\users\$env:USERNAME\desktop\URLs.csv" -NoTypeInformation

then you could Add yourself using:

#Connect to SharePoint Admin Center using the SPO module
$creds = Get-Credential
Connect-SPOService -Url https://<TENANT_NAME> -Credential $creds

#Import users
$userURLs = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\URLs.csv"

#Store 2nd Admin account into a variable
$adminAcctToAdd = "email address removed for privacy reasons"

#Add 2nd Site Collection admin
foreach ($url in $userURLS) {
Write-Host "Connecting to: $($url.PersonalUrl) and adding user $($adminAcctToAdd)" -ForegroundColor Green
Set-SPOUser -Site $url.PersonalUrl -LoginName $adminAcctToAdd -IsSiteCollectionAdmin $true

And create folders like this:

#Import URLs
$userURLs = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\URLs.csv"

#Add a folder in each ODFB
foreach ($url in $userURLs) {
Connect-PnPOnline -Url $url.PersonalUrl
Add-PnPFolder -Name "Folder1" -Folder "Documents"



Thanks for your help. The code works up until the point below:


$ODFBurls = @() foreach ($user in $myUsers) { $ODFBurls += Get-PnPUserProfileProperty -Account $user.UserPrincipalName } $ODFBurls | Select-Object PersonalUrl | Export-Csv -Path "C:\users\$env:USERNAME\desktop\URLs.csv" -NoTypeInformation


It creates the URL file, but no URLs are included in it.

The "UserToPreProv" file contains two accounts with the header UserPrincipalName followed by the emails. I import it using the command from your instructions: 

$myUsers = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\UsersToPreProv.csv"


Where am I going wrong? Unless the URLs come later, I run into an issue where PowerShell 7 does not recognise Connect-SPOService.

Thanks again for your assistance.




Screenshot 2024-07-11 at 08.28.36.png

Screenshot 2024-07-11 at 08.24.06.png


Did you use "Import-Module -Name pnp.powershell -UseWindowsPowerShell" in PS7?

Hi @Harm_Veenstra 


I tried again but encountered issues with the command you mentioned. However, I was able to run Import-Module -Name pnp.powershell.


Here is the error I receive when using Import-Module -Name pnp.powershell -UseWindowsPowerShell:


Screenshot 2024-07-16 at 09.38.21.png

I checked my PowerShell version, and it is: 


Screenshot 2024-07-16 at 09.39.21.png

When I attempt to use Connect-SPOService, I get a "command not recognized" error.


Do you have any ideas on how to resolve this?

Thank you.