user1133737
user1133737

Reputation: 113

How to use Test-Path in SharePoint

Using the Test-Path PowerShell command we can check if the system drive path or file exists or not in local drive. Similarly how can we check if the SharePoint document library folder path or file in document library exists or not using Test-Path or any similar command?

Test-Path -Path "http://win-3:001/sites/Dev/Shared%20Documents/Test1"
Test-Path -Path "http://win-3:001/sites/Dev/Shared%20Documents/Test1/sample.txt"

Upvotes: 0

Views: 1700

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

You should be able to check that with an HTTP request:

$uri = 'http://win-3:001/sites/Dev/Shared%20Documents/Test1'

(Invoke-WebRequest -Method Head -Uri $uri -UseDefaultCredentials).StatusCode

If your PowerShell version is too old to provide the Invoke-WebRequest cmdlet you should upgrade. If for some reason you can't do that use the System.Net.WebRequest class instead:

$uri = 'http://win-3:001/sites/Dev/Shared%20Documents/Test1'

$req = [Net.WebRequest]::Create($uri)
$req.Method = 'HEAD'
$req.UseDefaultCredentials = $true
$req.PreAuthenticate = $true
$req.Credentials = [Net.CredentialCache]::DefaultCredentials
$req.GetResponse().StatusCode.value__

Either way, a status code of 200 means the request was OK, i.e. the document exists.

Upvotes: 1

Related Questions