Forum Discussion
Using powershell to create folders within users onedrive
- Aug 22, 2024You could use this changed bit, added the add and remove permissions in the loop
# Add a folder in each ODFB
foreach ($url in $userURLs) {
Set-PnPTenantSite -Url $URL.PersonalUrl -Owners $SiteCollAdmin
Connect-PnPOnline -Url $url.PersonalUrl
Add-PnPFolder -Name "Folder1" -Folder "Documents"
Remove-PnPSiteCollectionAdmin -Owners $SiteCollAdmin
}
Not tested 😄 - AB21805Aug 22, 2024Bronze Contributor
Hi
I've been using Connect-PnPOnline, and everything seems to be working except for exporting URLs into a CSV, which isn't functioning at all. It used to work fine in previous years, so I'm unsure what's causing the issue now. As a temporary solution, I've gained access to all OneDrives, rather than just the specific URLs I need.
Here's the script I used to gain access:
# Set Parameters $AdminSiteURL = "https://tenant-admin.sharepoint.com" $SiteCollAdmin = "email address removed for privacy reasons" # 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)" }
Since the URLs are displayed in the PowerShell window, I’m manually copying them into the URLs.csv file, which should have been automatically populated according to this guide. I then remove the URLs I don’t need and run the following script:
# 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" }
This method is a bit cumbersome but has been effective in creating the folders. I'll also need to remove the Admin permissions from all OneDrives afterward, and I’m hoping the following script will do the trick:
# Set Parameters $AdminSiteURL = "https://tenant-admin.sharepoint.com" $SiteCollAdmin = "email address removed for privacy reasons" # 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 } }
I'll keep you updated on whether this works. In the meantime, do you know of a way to manage permissions only for the specific OneDrives I need, without granting access to all? Could you also check if you encounter the same issue with the CSV file not populating with URLs, just in case I’m overlooking something?
Thanks in advance!
- Jul 19, 2024
You could switch from connect-sposervice to connect-pnponline?
PnP PowerShell vs SharePoint Online Management Shell
Before we are going to take a look at how to use PnP PowerShell, it’s good to know that there is also a PowerShell module from Microsoft. The SharePoint Online Management Shell (SPO) is the official PowerShell module to manage your SharePoint Online environment.
So what are the differences between the two? Let’s take a brief look:
- SPO is for managing SharePoint at the Tenant level, whereas PnP PowerShell allows you to connect to a specific SharePoint site
- PnP PowerShell cmdlets contain more parameters, which allows you to achieve more (complex) tasks when compared to equivalent SPO cmdlets.
- SPO cmdlets run within the Tenant Admin rights, whereas PnP PowerShell runs the context of the current user (you could of course connect with admin credentials)
Each has its own advantages. If you want to run commands on a specific SharePoint site or within the current user context, go with PnP PowerShell. Do you need to change settings at the Tenant level, use the SPO cmdlets.
C:\Users\HarmVeenstra> Get-Command -Module pnp.powershell -Verb connect
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Connect-PnPOnline 2.5.0 pnp.powershell - AB21805Jul 19, 2024Bronze Contributor
This did not work, I did reply with a screenshot showing I tried the same:
I checked my PowerShell version, and it is:
When I attempt to use Connect-SPOService, I get a "command not recognized" error.
- Jul 17, 2024Can you import it without the -UseWindowsPowerShell parameter?
- AB21805Jul 17, 2024Bronze Contributor
- Jul 16, 2024Install-Module -Name Microsoft.Online.SharePoint.PowerShell, does that fix it?
- AB21805Jul 16, 2024Bronze Contributor
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:
I checked my PowerShell version, and it is:
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.
- Jul 15, 2024Did you use "Import-Module -Name pnp.powershell -UseWindowsPowerShell" in PS7?
- AB21805Jul 11, 2024Bronze Contributor
Hi
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" -NoTypeInformationIt 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.
- Jul 10, 2024If 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>-admin.sharepoint.com -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"
} - AB21805Jul 10, 2024Bronze Contributor
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.
- Jul 10, 2024New-PnPPersonalSite creates the site, but the site is already there. You can follow the steps in the article to achieve what you want?
- AB21805Jul 10, 2024Bronze ContributorNo as I have tried to follow this: https://veronicageek.com/2019/create-folders-in-odfb/#pre-provision-each-users-onedrive-for-business
The steps in the link, permissions are done after the URLs are exported.