Solved

Script to create folders and sub folders to diffrent document libaries

Posted on 2013-10-22
2
4,011 Views
Last Modified: 2013-10-24
Experts,
i have a script where i can run to create folder and a subfolder. The script works perfectly for one document library. If i have a subsite in my sharepoint 2013 and many document libraries, all starts with library* , how can the script check and create folders if needed

Here is the script i have which works on ONE document library. As soon as i change the $List=$web.Lists | where {$_.Title -like "Libra*"} it stop working. i assume it may have something to do with foreach command. Here is the script

#Get the Web
$web=Get-SPWeb "http://portal.domain.com/subsite"
#Get the List/Library
$List=$web.Lists | where {$_.Title -like "Libra*"}
$foldername = Get-Date -Format "yyyy MMMMM"

foreach($L in $List) {

 }
if($List)  #Check If List exists! }


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


   }
 }

$web.Dispose()
0
Comment
Question by:stratoman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
stratoman earned 0 total points
ID: 39595558
we rewrote the script but still not creating folder and sub folders in document libraries

$web = Get-SPWeb "http://portal.domain.com/subsite"
 $Libraries = $web.Lists | where {$_.Title -like "Libra*"}
 $foldername = Get-Date -Format "yyyy-MMMMM"
 
 foreach ($library in $Libraries) {
 
 Write-Output "Getting Library $($library.Title)"    
         # Check Folder Doesn't exists in the Library!
         $folder = $library.ParentWeb.GetFolder($library.RootFolder.Url + "/" +"Archives");}
 
#Use the ! to signify not, cleaner code to determine if the folder is a variable rather than a null
  if (!$folder)
    {
           $folder = $library.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Archives")
           $folder.Update();
           write-host "Folder created"
}
else
{
 write-host "Folder already exists"
}
         #Check if sub-folder doesn't exists already
         $Subfolder = $library.ParentWeb.GetFolder($folder.URL + "/" + $foldername);
#The former for-each loop was in the wrong place.
 
#Again this has been shortened to make cleaner
 if (!$Subfolder)  
 {
           #Create a folder Inside "Archives"
           $Subfolder = $library.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $foldername)
                  $Subfolder.Update();
  write-host "Subfolder created"
}
else
{
  write-host "Subfolder already exists"
}
0
 

Author Closing Comment

by:stratoman
ID: 39597592
closing this
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question