0

MyList is a document library with a column called 'name'. I am trying to do this in my Powershell script:

$myList= $subWeb.Lists["MyList"]
$items= $myList.Items | sort Title

foreach ($li in $items) {
    Write-Host $li.name # is empty?
}

When I look in the interface it clearly shows rows in the MyList library. Why is $li.name empty or how can I get the $li.name property?

3 Answers 3

0
$myList = $subWeb.Lists["MyList"]
$item s= $myList.Items | sort Title

foreach ($li in $items) {
    Write-Host $li.["Name"]
}
0

I think what you are looking for is the file name.

It resides in an internal hidden column FileLeafRef.

Modify your code as below:

$myList = $subWeb.Lists["MyList"]
$items = $myList.Items | sort Title

foreach ($li in $items) {
    Write-Host $li["FileLeafRef"] #should output something like - test.docx
    #to update list item
    $li["FileLeafRef"] = "newFileName.docx"  #change it as per your requirement
    $li["Title"] = "New document title"
    $li.Update();
}
3
  • 1
    Would be a shame if there were more than one document with that filename and extension or other filetypes than docx in the library, don’t you agree? ;) Commented Apr 12, 2018 at 12:15
  • I tried $li["description"] for another column which contains a custom string value and still is empty? Commented Apr 12, 2018 at 12:30
  • @Christoffer - true that ;) @bierhier - You should ideally use internal column name in PowerShell, check if the name is something like $li["Description"] and also check if the column itself has some value. Commented Apr 12, 2018 at 12:33
0

You need to go to Library settings->Columns, and find the column and click it.

Then you will find the internal name of the field via Field parameter of the URL: enter image description here

Then in your PowerShell commands, you need to use the internal name.

A demo for your reference:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

$SPweb = Get-SPWeb "http://your site url"
$SPlist = $SPweb.Lists["your library"]
$items=$SPlist.Items | sort Title

foreach ($SPitem in $items)
{
    write-host $SPitem["name1"]

}

Note: you need to change "name1" to the internal name of your column.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.