0

I used some PowerShell code to create a new folder on a custom list where I have folders enabled. The strange thing I cant seem to figure out is that the Folder in the list seems to have two properties "name" that displays in the properties pages and URL and "title" that displays in the list view.

In the screen shots below is what my result is so I was just looking to better understand what is going on so that I can also update the folder display title as well.

(Outside folder created with PowerShell)

enter image description here

(Inside folder created with PowerShell)

enter image description here

By the way the URL's for this ended up working out fine (site/Lists/test1/Root-Folder)

Here is a sample of the code I used so far:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


# Get Site
$Site = Get-SPSite -Identity "https://spSite.com/sites/";

## Get Web <web url>
$Web = $Site.OpenWeb("site1");

## Get List <list name>
$list = $Web.Lists["test1"];

if($list)  #Check If List exists!
 {
  # Create a Folder "Root-Folder"
  # Check Folder Doesn't exists in the Library!
  $folder = $list.ParentWeb.GetFolder($list.RootFolder.Url + "/" +"Root-    Folder");

  if ($folder.Exists -eq $false)
   {
    #Create a Folder
    $folder = $list.AddItem("",     [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Root-Folder")
    $folder.Update();
   }

  #Create a Sub-Folder "Sub-Folder" inside "Root-Folder"
  #Check if sub-folder doesn't exists already
  $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "Sub-Folder");
  if ($Subfolder.Exists -eq $false)
   {
    #Create a Sub-Folder Inside "Root-Folder"
    $Subfolder = $list.AddItem($folder.URL,     [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Sub-Folder")
    $Subfolder.Update();
   }
 }

Also on a side note if I manually edit and save the folder in SharePoint after the fact the issue goes away.

1

I would make a slight modification to the script. Instead of naming the folder in the AddItem method as parameter, I would add it as Title before making the Update()

$Subfolder = $list.AddItem($folder.URL,  
                    [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$Subfolder["Title"] = "Root-Folder"
$Subfolder.Update();

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.