Forum Discussion
Using powershell to create folders within users onedrive
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.
- 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