0

I have a powershell script that display a list of files that are checked out all users in a site and exports the output as a csv file:

 Add-PSSnapin Microsoft.Sharepoint.Powershell
$url = "contoso.com"
$site = New-Object Microsoft.SharePoint.SPSite($url)
$web = $site.OpenWeb()
$list = $web.Lists

function Get-DocInventory() {   

                $web = Get-SPWeb "http://portal.contoso.com"

                    foreach ($list in $web.Lists) {

                        if ($list.BaseType -eq "DocumentLibrary") {

                         $dList = [Microsoft.Sharepoint.SPDocumentLibrary]$list
                         $items = $dList.Items
                         $files = $dList.CheckedOutFiles

                        foreach($file in $files){

                           foreach($item in $items){

                                if($item["Checked Out To"] -ne $null){

                             $wuse = $file.DirName.Substring($web.ServerRelativeUrl.Length)                          
                             $modifiedTime = $web.RegionalSettings.TimeZone
                             $data = @{
                                "Version" = $item["Version"]
                                "library" = $wuse
                                "URL" = $web.Url + ('/') + $item.Url
                                "Title" = $file.LeafName
                                "CheckedOut By" = $file.CheckedOutBy.Name
                                "Time last Modified" = $modifiedTime.UTCToLocalTime($file.TimeLastModified)

                            }
                           }

                              New-Object PSObject -Property $data

                            }
                          }                        
                       }
                    }
                    $web.Dispose();
                }
                $site.Dispose()

        }

Get-DocInventory | Export-Csv -NoTypeInformation -Path D:\Docinventory.csv

I have an example of what the script outputs as shown below: enter image description here

My question is, can someone please help/tell me why for the example shown above, it duplicates the file (Orangutans.docx) multiple times? In addition, the output displays the incorrect URL. The putout should only display one URL and it should be http://contoso.com/sites/Depts3/KDS/RP1MajorVersions/Orangutans.docx.

1 Answer 1

1

If you only need the checked-out files, then I believe you should not loop over the list items in your code i.e. remove foreach($item in $items) which is present inside foreach($file in $files). Moreover, if you want to get the SPListItem associated with the SPFile, you can use SPListItem.File property for that.

2
  • So in essence, you're advising me to replace the line foreach($item in $items) with SPListItem.File? Please advise.
    – Nina G
    Commented Oct 9, 2015 at 17:02
  • That should be the first step. But I haven't tested your script. Commented Oct 10, 2015 at 3:24

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.