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.


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"}
| improve this answer | |
  • what is the output returns by Add-PnpFile i.e what is in $f object? – nbi Oct 30 '18 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 '19 at 21:31

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.