I tried this script

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Start-SPAssignment -Global
$Web = 'http://test'
$SPWeb = Get-SPWeb -Identity $Web
write-Host $Web
$listTemplate = $SPWeb.ListTemplates["Picture Library"]
$list = $SPWeb.Lists[$ListUrl]
$list.Title = $ListTitle
Stop-SPAssignment -Global

to create a Picture library, But It throws an error message

**Get-SPWeb : Cannot find an SPWeb object with Id or Url :****

And I need to create it for different subsites also, I am not sure how to achieve this, Please suggest me some idea?

I referred this links

How to create Picture Library on office365 sharepoint using powershell?

  • have u opened the client context? Jan 16, 2017 at 8:28

2 Answers 2

function Create-SharePointLibrary { 
       [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]    
       [Parameter(Mandatory=$true, Position=1)]   
       [Parameter(Mandatory=$true, Position=2)]
       [Parameter(Mandatory=$false, Position=3)]
      Start-SPAssignment -Global    
      $spWeb = Get-SPWeb -Identity $webUrl       
      $spListCollection = $spWeb.Lists    
      $spLibrary = $spListCollection.TryGetList($LibraryName)  
      if($spLibrary -ne $null) {
          Write-Host -f Yellow "Library $LibraryName already exists in the site"

      } else {           
          Write-Host -NoNewLine -f yellow "Creating  Library - $LibraryName"   
          $spListCollection.Add($LibraryName, $Description, $LibraryTemplate)    
          $spLibrary = $spWeb.GetList($spWeb.ServerRelativeUrl+"/"+"$LibraryName") 
          Write-Host -f Green "...Success!"    
      Stop-SPAssignment -Global  

Calling Function

$webUrl = "http://sever:port"
#-----------Picture Library------------
$PicLibName = "Pictures"  
$LibraryTemplateDL = [Microsoft.SharePoint.SPListTemplateType]::PictureLibrary
Create-SharePointLibrary $webUrl  $PicLibName $PicLibName $LibraryTemplateDL

Try it as below:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Start-SPAssignment -Global
$SPWeb = Get-SPWeb -Identity "http://test"
$listTemplate = $SPWeb.ListTemplates["Picture Library"]
$SPWeb.Lists.Add("Custom Picture Library","Custom Picture Library desc",$listTemplate)
$list = $SPWeb.Lists["Custom Picture Library"]
$list.Title = "Custom Picture Library"
Stop-SPAssignment -Global
  • Thanks, while running the script it throws an error abe02e3c-e792-4692-884c-d78463545712 Index operation failed; the array index evaluated to null. At C:\Users\Administrator\desktop\Picturelibrary.ps1:6 char:1 + $list = $SPWeb.Lists[$ListUrl] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    – Debugger
    Jan 16, 2017 at 9:43
  • But Picture Library is created @GautamSheth
    – Debugger
    Jan 16, 2017 at 9:44
  • Edited code, can you please check ? Jan 16, 2017 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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