I have the following piece of code to break role inheritance in specific folder and apply another role

$folder= $web.GetFolder($folderURL)

Success it works fine when I set cleaned url

Failure It failed in both:

  1. I copy full url from browser for the same folder Test4 and set it to $folderURL $folderURL=http://thalabapp01:10574/sites/SiteCollection1/_layouts/15/start.aspx#/DocummentLibrary1/Forms/AllItems.aspx?RootFolder=%2Fsites%2FSiteCollection1%2FDocummentLibrary1%2FTest3%2FTest4&FolderCTID=0x012000CB838094FA7E5547AE67B658B37A595B&View=%7B80AA6D44%2DD5AD%2D496B%2D87C7%2D660CEB513A92%7D

  2. Used folder in shared documents http://thalabapp01:10574/sites/SiteCollection1/Documents/Test2

The error is : You cannot call a method on a null-valued expression. At D:\GrantPermissions\GrantPermission.ps1:9 char:4 + $folder.Item.BreakRoleInheritance("true") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

Please advice !

1 Answer 1


The SPWeb.GetFolder method accepts server-relative URL for the folder. This is the reason why your 'clean' url works and your 'browser' url doesn't.

The error you are getting for 'shared documents' folder most likely indicates that this folder doesn't exist. Try pasting the url in browser to verify it.

It is always smart to check if the folder you are trying to get exist before you try to access it. So your code should be something like this:

$folder= $web.GetFolder($folderURL)
if($folder.Exist -eq $true) {

Also in PS it isn't "true". It is $true.


Only way to use 'browser' url is to extract folder server-relative URL from it. The code is not generic so I cannot guarantee that it will work for all url formats:

$browserUrl = "http://thalabapp01:10574/sites/SiteCollection1/_layouts/15/start.aspx#/DocummentLibrary1/Forms/AllItems.aspx?RootFolder=%2Fsites%2FSiteCollection1%2FDocummentLibrary1%2FTest3%2FTest4"
[URI]$url = $browserUrl
#It is a bit difficult to extract the RootFolder from this Url
$start = $browserUrl.IndexOf("RootFolder=") + 11
$end = $browserUrl.Substring($start, $browserUrl.Length - $start).IndexOf("&")
if($end -eq -1) {$end = $browserUrl.Length - $start}
$folderURL = $url.Scheme + "://" + $url.Authority + [System.Web.HttpUtility]::UrlDecode($browserUrl.Substring($start, $end))
# in this case $folderUrl = http://thalabapp01:10574/sites/SiteCollection1/DocummentLibrary1/Test3/Test4
  • Thanks Vedran , Is there ay other way to work with full browser url ?shared documents folder is existed ? Commented Mar 18, 2015 at 11:58
  • @thabet084 I have updated my answer. I cannot answer you on your shared folder issue because it is environment specific (either you are using wrong url or folder doesn't exist). If you find my answer to be helpful please upvote it and/or accept it. Commented Mar 18, 2015 at 12:32

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.