I am new in PowerShell. I am trying to print the Library title using PowerShell script. The Library names are stored in XML.

      Test Library 1
      Test Library 2

The Powershell script is as below:

$xmlPath=Read-Host 'Enter xml file physical path:'
$xmlinput = [xml](get-content $xmlPath)
$item = $xmlinput.Config  
$AllLibraries = $item.Libraries

foreach($webApp in $AllLibraries.ChildNodes)
        $web = Get-SPWeb "Site URL"
        $LibraryName = $webApp.InnerText
        $list = $web.Lists[$LibraryName]
        Write-Host -NoNewline $list.Title
    catch [Exception]
      Write-Host -NoNewline $_.Exception.GetType().FullName, $_.Exception.Message -ForegroundColor Red

When I write $list = $web.Lists["Test Library 1"] then it worked fine, but when I try to access like this $list = $web.Lists[$LibraryName] then it gives nothing.

What may I missing?

  • Insert "Write-Host $LibraryName" to check your value. – Akshay Belure Sep 6 '16 at 7:00
  • That was perfectly displayed the name of library. But I got the solution by applying Trim function at the end. There might be issue with extra space in xml – Aakash Maurya Sep 6 '16 at 7:03

Just modify one line and your issue will be resolved:

$list = $web.Lists[$LibraryName.Trim()]

  • Hey you guessed perfectly true. But can you please let you know me how you thought of this mistake? – Aakash Maurya Sep 6 '16 at 6:58
  • 2
    It's not guess dear.. i have tested your powershell in my environment and after some testing and printing some lines, i have realised that some spaces are there.. so trimmed it.. – SID Sep 6 '16 at 7:03

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.