Forum Discussion

jambou9182's avatar
jambou9182
Copper Contributor
May 20, 2024

Create folders in library using powershell and a csv file and color the folders red.

I'm using this script below to create folders based on values from a csv file.  This script works but I also need to color the folders Red, instead of the default yellow.  Can anyone help me add that to the script?

 

#Config Variables
$SiteURL = "https://MYTENANT.sharepoint.com/sites/TestSiteForTemplate1/"
$CSVFilePath = "C:\SharePointScripts\DocumentLibraries\FolderNames.csv"
$LibraryName = "Library 1"

Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
$Web = Get-PnPWeb

#Get the Document Library and its site relative URL
$Library = Get-PnPList -Identity $LibraryName -Includes RootFolder
If($Web.ServerRelativeUrl -eq "/")
{
$LibrarySiteRelativeURL = $Library.RootFolder.ServerRelativeUrl
}
else
{
$LibrarySiteRelativeURL = $Library.RootFolder.ServerRelativeUrl.Replace($Web.ServerRelativeUrl,'')
}

#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath

#Read CSV file and create folders
ForEach($Row in $CSVFile)
{
#Replace Invalid Characters from Folder Name, If any
$FolderName = $Row.FolderName
$FolderName = [RegEx]::Replace($FolderName, "[{0}]" -f ([RegEx]::Escape([String]'\"*:<>?/\|')), '_')

#Frame the Folder Name
$FolderURL = $LibrarySiteRelativeURL+"/"+$FolderName

#Create Folder if it doesn't exist
Resolve-PnPFolder -SiteRelativePath $FolderURL | Out-Null
Write-host "Ensured Folder:"$FolderName -f Green
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

No RepliesBe the first to reply

Resources