I have the following script that creates a folder named as yesterday's date under the list "Documents". However, I need to create this folder under another subfolder called "FinancialReports" instead. Please could someone help me out?

$FolderName = ((get-date).adddays(-1)).tostring('yyyy-MM-dd') 
$User = "me@mydomain.com"  
$Password = "Password321"
$SiteURL = "https://mydomain.sharepoint.com/sites/Accounts/"
$LibraryName = "Documents"

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo-SecureString $Password -AsPlainText -Force))  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the Library by Name
$List = $Ctx.Web.Lists.GetByTitle($LibraryName)

#sharepoint online create folder powershell
    $NewFolder = $List.RootFolder.Folders.Add($FolderName)


So i ended up using PNP which made things a lot easier.

Added PNP with one command

"Install-Module SharePointPnPPowerShellOnline"

Here's the guide: https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps

my finished code is now:

$FolderName = ((get-date).adddays(-1)).tostring('yyyy_MM_dd') 
$User = "me@mydomain.com"  
$Password = "Password321"
$SiteURL = "https://mydomain.sharepoint.com/sites/Accounts/"

$spfolder = "shared documents/FinancialReporting/$FolderName"

$sp = $password | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object system.management.automation.pscredential -ArgumentList $user, $sp

Connect-PnPOnline -Url $SiteUrl -Credentials $Cred  
# the default "Documents" list = "shared documents" for some reason...
Add-PnPFolder -name $FolderName -folder "shared documents/FinancialReporting" 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.