Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
2.9k views
in SharePoint Server by 31 37 42
Using PowerShell, How can I check if the file already exists in the SharePoint document library without the need to loop for each file?

1 Answer

1 like 0 dislike
by 163 204 403
selected by
 
Best answer

In the SharePoint server, you have to use GetListItem($FileURL) to check if a file exists in a SharePoint document library without looping using PowerShell

Script

$siteURL = "SiteURL" #provide the site url 
$FileURL = "DocumentURL" #provide the document URL

try{
$Found = (Get-SPWeb $siteURL).GetListItem($FileURL)
#if the file exists return the file name
If ($Found -ne $null){
  write-host "The file $($Found.Name) is found"}
}
#if file not found, it will raise an error, so we use try-catch
catch{ write-host "The file is not found"}

Output

check if file exists in sharepoint powershell

If you don’t ask, the answer is always NO!
...