1

I uploaded a file to SP library with $f = Add-PnPFile -Path "C:\temp\01.pdf" -Folder "documents"

What is best way to get the id or UniqueID of this new file? I need this info to use Set-PnPListItem command.

2 Answers 2

2

Not sure why you need Set-PnPListItem after adding the file.

To add the properties of the file, you can just use the Values parameter and set them as below:

$f = Add-PnPFile -Path "C:\temp\01.pdf" -Folder "Documents" -Values
@{Title="01 Title";Description="Test description"}

Reference- Add-PnPFile

However, if you do need to use Set-PnPListItem directly after uploading the file, you can use it as below. We will first upload the file, then fetch it using the latest item id and then update it.

$f = Add-PnPFile -Path "C:\temp\01.pdf.pdf" -Folder "Documents"
$listItems = Get-PnPListItem -List "Documents"
$maxItemId = ($listItems.Id | measure-object -maximum).maximum;
Set-PnPListItem -List "Documents" -Identity $maxItemId -Values @{Title="01 Title";Description="Test description"}

or using Get-PnPFile to get list item and then update as below:

$f = Add-PnPFile -Path "C:\temp\01.pdf" -Folder "Documents"
$listItem = Get-PnPFile -Url "/sites/test/Documents/01.pdf" -AsListItem
$res = Set-PnPListItem -List "Documents" -Identity $listItem -Values @{Title="01 Title";Description="Test description"}
3
  • what is the output returns by Add-PnpFile i.e what is in $f object?
    – nbi
    Oct 30, 2018 at 5:29
  • Using the maximum id as shown could result in a false match. For example, if a file was added in the time between add-pnpfile and the time Get-PnPListItem takes to return.
    – Tom Resing
    May 11, 2019 at 21:31
  • thx! the -AsListItem is exactly what i was looking for! Aug 19, 2021 at 23:26
0
$file = Add-PnPFile -Path "C:\temp\01.pdf" -Folder "Documents";
$listItem = Get-PnPFile -Url $file.ServerRelativeUrl -AsListItem;
Set-PnPListItem -List "Documents" -Identity $listItem["ID"] -Values @{Title="01 Title";Description="Test description"}

Your Answer

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

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