I have written this script based on some feedback I received in this article, which has become one of the more popular posts on the site. I was asked if there was any way of assigning group permissions for all sites in a site collection, bearing in mind that some sites will have unique permissions set on them whereas others will inherit their permissions from a parent site.
The function below provides this functionality, as well as a few other features:
- Skips sites where permissions are being inherited from a parent site
- Adds not only SharePoint groups to sites, but also Active Directory users and groups
- Provides the option to skip the root site of the site collection, should you only wish to set permissions on all sub-sites
- Will add a new SharePoint group to the site collection, if it doesn’t exist already. The script will allow you to add a description for the group, and it will automatically assign the user running the script as group owner and member
Before you can start assigning permissions to sites using the script, you have to run the function first in a PowerShell console with the SharePoint cmdlets loaded (e.g., the SharePoint 2010 Management Shell). I have annotated portions of the script so that you can hopefully follow what it is doing:
function AddAccountToAllSites ($siteURL, $accountName, $permLevel, [switch]$skipRootSite, $newGroupDescription)
{
#Get Site Collection
$site = Get-SPSite $siteURL
#Check if the accountName variable contains a slash - if so, it is an AD account
#If not, it is a SharePoint Group
$rootWeb = $site.RootWeb
if ($accountName.Contains("\")) { $account = $rootWeb.EnsureUser($accountName) }
else {
#If the SharePoint Group does not exist, create it with the name and description specified
if (!$rootWeb.SiteGroups[$accountName])
{
$rootWeb.SiteGroups.Add($accountName, $rootWeb.CurrentUser, $rootWeb.CurrentUser, $newGroupDescription)
}
$account = $rootWeb.SiteGroups[$accountName]
}
$rootWeb.Dispose()
#Step through each site in the site collection
$site | Get-SPWeb -limit all | ForEach-Object {
#Check if the user has chosen to skip the root site - if so, do not change permissions on it
if (($skipRootSite) -and ($site.Url -eq $_.Url)) { write-host "Root site" $_.Url "will be bypassed" }
else {
#Check if the current site is inheriting permissions from its parent
#If not, set permissions on current site
if ($_.HasUniqueRoleAssignments) {$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $_.RoleDefinitions[$permLevel]
$assignment.RoleDefinitionBindings.Add($role)
$_.RoleAssignments.Add($assignment)
write-host "Account" $accountName "added to site" $_.Url "with" $permLevel "permissions."
}
else {
write-host "Site" $_.Url "will not be modified as it inherits permissions from a parent site."
}
}
}
#Display completion message and dispose of site object
write-host "Operation Complete."
$site.Dispose()
}
Once the script has been run, you can use it to assign permissions to your site collection by calling the function. Here are some scenarios:
- Add the Active Directory user PACDOMAIN\Phil to all sites except the root site and assign Read permissions to them:
AddAccountToAllSites -siteURL "http://portal" -accountName "PACDOMAIN\Phil" -permLevel "Read" -skipRootSite
- Add the Active Directory user PACDOMAIN\Phil to all sites including the root site and assign Read permissions to them:
AddAccountToAllSites -siteURL "http://portal" -accountName "PACDOMAIN\Phil" -permLevel "Read"
- Add the Active Directory group PACDOMAIN\Portal Users to all sites including the root site and assign Read permissions to it:
AddAccountToAllSites -siteURL "http://portal" -accountName "PACDOMAIN\Portal Users" -permLevel "Read"
- Add the SharePoint group “Test Group” to all sites except the root site and assign Full Control permissions to it. I am also assuming that this group has already been created in the site collection:
AddAccountToAllSites -siteURL "http://portal" -accountName "Test Group" -permLevel "Full Control" -skipRootSite
- Add the SharePoint group “Test New Group” to all sites except the root site and assign Contribute permissions to it. This time I would like to create the group in the site collection as it doesn’t currently exist, and so I am also specifying the group description to be used during creation:
AddAccountToAllSites -siteURL "http://portal" -accountName "Test New Group" -permLevel "Contribute" -skipRootSite -newGroupDescription "This is a test group"
The screenshot below shows the affect of running these commands on one of the sites configured with unique permissions. All sites inheriting permissions will not be changed, although they will inherit these changes if their parent site has been affected by them.
I have posted a follow up article demonstrating how users and group assignments can be removed from sites in a similar way.
Will this script also work in Sharepoint MOSS 2007?
ReplyDeleteGreat Article
DeleteCyber Security Projects for CSE Students
JavaScript Training in Chennai
Project Centers in Chennai
JavaScript Training in Chennai
Yes, with a few tweaks:
ReplyDeletehttp://get-spscripts.com/2011/03/using-powershell-scripts-with-wss-30.html
This is a potentially awesome solution to what I'm trying to accomplish, but it simply stops without executing anything for me. Just blanks.
ReplyDeleteI'm trying to tweak this code a bit and hopefully I can get it to work.
Scratch that. I see what I was doing wrong. I actually ended up adding this function to another script and running it from the new script. Thanks!
ReplyDeleteHi, I have the same problem. Shall I insert the scenario end the end of the first script?
DeleteThanks
Hi Phil
ReplyDeleteGreat post!
I need to update the permissions for a particular site and subsites with in a site collection. i.e. we have 1 site collection which contains all of our departments, i only need to update the permissions for 1 department. Is this possible with this script?
Many thanks!
Tomas
Tomas - You can use elements of this script to do it just not the script as it is written here. You may also want to look at this one for inspiration: http://get-spscripts.com/2010/07/adding-groups-with-permission-levels-to.html
ReplyDeleteI've improved upon the script a bit, just a few minor changes. If you're interested, email me - "john at dumb dot org" or email my gmail id in my comments.
ReplyDeleteWonderful blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks.sbo
ReplyDeleteHello Phil,
ReplyDeleteVery useful script. Is there a way to use it to add a user when using claims-based authentication? If I just add the user name they show up in the site's permissions list, but don't actually have any permissions. When added through the GUI the username is prefaced with 'i:0#.w'.
OK solved it - needed to put a pipe so "i:0#.w|domain\user"
DeleteHi Phil,
ReplyDeleteJust an off the topic question taht I'm looking for an answer... I'm writing a program to grant user permission to a folder. this is for Sharepoint 2007 and I'm using c# managed code. (not powershell) However I am unable to add user permissions to a folder in a document library unless the user is added to the site permissions. Am I missing something here? or is this a known issue in MOSS and if so is there a workaround available?
Is's great!
ReplyDeleteExist any like this for adding permission for all non-inherited content objects(libraries, lists, folders, documents, ...) in specified site?
Thank you
Josef
I have 300 folders under a site. There is a subfolder that has to have unique permissions under each of these 300 folders. Anyway to run a script to add unique permissions to the subfolder without having to break the inheritance manually and change the permissions?
ReplyDeleteThanks a lot for the script... You saved my day!!!
ReplyDeleteTrying to use this script but getting errors:
ReplyDeleteException calling "Add" with "1" argument(s): "Object reference not set to an i
nstance of an object."
+ $assignment.RoleDefinitionBindings.Add <<<< ($role)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Add" with "1" argument(s): "Cannot add a role assignment wit
h empty role definition binding collection."
+ $_.RoleAssignments.Add <<<< ($assignment)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Found what caused the error, had to use the Swedish term for "Full Control" because Sharepoint site is in Swedish.
Delete2015-12-7 xiaozhengm
ReplyDeleteugg boots
hollisters
michael kors handbags
nike free runs
sac longchamp pas cher
christian louboutin outlet
true religion
gucci borse
fitflop uk
christian louboutin
michael kors
michael kors outlet
ralph lauren uk
cheap uggs on sale
barbour uk
jordan 4
juicy couture
jordan 6
michael kors outlet
ed hardy outlet
ralph lauren pas cher
nike shoes
gucci outlet
ray ban sunglasses
michael kors outlet online
coach factory outlet
christian louboutin outlet
mizuno running shoes
oakley vault
nike roshe run women
cheap jordans
ugg boots
coach outlet
michael kors
nike outlet store
michael kors outlet online
coach outlet
jordan 13
pandora charms
coach outlet
Great Article..
ReplyDeleteOnline DotNet Training
.Net Online Training
Dot Net Training in Chennai
IT Training in Chennai
jianbin0309
ReplyDeletetrue religion jeans outlet
celine outlet
louis vuitton handbags outlet
air jordan shoes for sale
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
tiffany outlet
swarovski crystal
michael kors outlet store
michael kors clearance
hermes bags
ray ban sunglasses
marc jacobs
valentino outlet
swarovski crystal
mac cosmetics
cheap nba jerseys
ray ban sunglasses
louis vuitton handbags outlet
ray-ban sunglasses
michael kors outlet store
michael kors outlet online
swarovski outlet
chicago blackhawks
true religion canada
michael kors outlet
ed hardy clothing
longchamp handbags outlet
prada outlet
ralph lauren shirts
michael kors factory store
cheap nfl jersey
rolex watches for sale
cheap nike shoes
mbt shoes outlet
coach factory outlet
ReplyDeletelouis vuitton handbags
louis vuitton outlet
kate spade handbags
timberland outlet
christian louboutin
louis vuitton handbags
polo shirts
fitflops sale clearance
burberry outlet
nike basketball shoes
coach outlet
vans shoes
air jordans
michael kors outlet online
louis vuitton outlet
polo ralph lauren
oakley vault
mont blanc pens for sale
nike factory outlet
fitflops
coach outlet
ray ban sunglasses outlet
cheap oakley sunglasses
cheap rolex watches
louis vuitton
louis vuitton outlet
oakley sunglasses
ghd flat iron
polo ralph lauren outlet
ralph lauren polo
nike roshe flyknit
coach outlet store online
louis vuitton outlet
adidas stan smith
longchamp outlet
louis vuitton outlet
pandora jewelry
coach outlet store online clearances
burberry outlet
20168.8wengdongdong
cheap ugg boots
ReplyDeleteugg boots outlet
jordan femme pas cher
patriots jerseys
beats headphones
ugg australia
michael kors outlet online
cheap rolex watches
true religion jeans
ray ban wayfarer
2016.11.26xukaimin
jordan retro
ReplyDeleteray ban sunglasses
kate spade outlet
replica watches
chanel handbags
michael kors handbags
oakley sunglasses
jordan 3
hollister clothing
ray ban sunglasses
20173.9chenjinyan
ray ban outlet
ReplyDeletehermes handbags
cheap ray bans
michael kors outlet
polo ralph lauren
coach outlet
le coq sportif shoes
ray ban eyeglasses
adidas originals superstar
new balance shoes
hzx20170415
vans outlet
ReplyDeleteed hardy jeans
kobe 12 shoes
christian louboutin sneakers
coach outlet store online
oakley outlet
henrikh mkhitaryan jersey
longchamp purse
ralph lauren polo outlet
michael kors handbags
2017.5.15chenlixiang
prada outlet online
ReplyDeletecheap nike shoes
polo ralph lauren outlet
nike air force
wireless beats headphones
fake rolex watches
rangers jerseys
nike free run flyknit
coach factory outlet online
christian louboutin sale
170517yueqin
شركة تنظيف مجالس بعنيزة
ReplyDeleteشركة تنظيف فلل ببريدة
شركة تنظيف فلل بعنيزة
شركة تسليك مجاري ببريدة
شركة تسليك مجاري بعنيزة
شركة جلي بلاط بعنيزة
شركة كسف تسربات المياة ببريدة
ReplyDeleteشركات الشحن جده مصر
شركات الشحن بجده لمصر
شركات الشحن مكه مصر
شركات الشحن جده لمصر
شركات الشحن مكه لمصر
شركة شحن بجده لمصر
شركات الشحن الرياض مصر
شركة شحن الرياض مصر
شركات الشحن
شركات الشحن الدولي
شركات شحن من الرياض لمصر
شركات الشحن الدولي
شركات شحن من الرياض لمصر
اسعار شركات الشحن
افضل شركة شحن
ارخص شركة شحن
شركات شحن لمصر بالرياض
شركات شحن بالرياض
شحن لمصر من الباب للباب
مكتب شحن لمصر
شحن لمصر
اسعار الشحن لمصر
Thank you so much, saved me hours of grinding monotony!
ReplyDeletepandora charms
ReplyDeletered bottoms louboutin
kobe 12
north face uk
valentino outlet
red bottom shoes}
nike factory
fitflop
cheap soccer cleats
supreme hoodie
3、
ReplyDeletechristian louboutin
birkenstock outlet
ferragamo outlet
fitflops sale clearance
reebok shoes
adidas outlet online
cheap jordans
coach outlet
ferragamo shoes
mowang05-27
Such a price provides important persuits functions, Duty, Broker as well bills. Specific degree is be more responsive to change if you do not make receipt. The local surf forecast in an european union registrant repeat apart from united kingdom, Signific tax about spend money on just isn't recoverable.
ReplyDeletePast rendering your company dfb trikot müller offer, maillot de foot personnalise You are investing buy these element from owner if you're the being successful prospective buyer. You Maillot De Foot Pas Cher read and be in Camisetas De Futbol Baratas accordance the worldwide shipping and delivery package t's and c's starts in a different home eyeport as well tabs. Significance costs beforehand estimated happen to be be more responsive to change within the raise you the optimal offer intensity.. amazon müller trikot
Caused by- rendering your prized wager, You are investing buy this advice element from the manuel neuer trikot rot owner if you're Maglie Poco Prezzo receiving prospective buyer. You read and agree with the worldwide shipping and delivery Maglie Da Calcio a Poco Prezzo show conditions parts in the home eyeport on the other hand case. Significance expenditure earlier estimated are almost always foreclosures Maglie Calcio Poco Prezzo change should you enhancement you juegos de futbol optimum wager range.
maillot de foot pas cher
ReplyDeletemaillot paris 2018
maillot de foot pas cher
Maillot Foot Pas Cher
maillot foot pas cher
maillot psg pas cher
maillot de foot pas cher
louboutin pas cher
louboutin soldes
Thank you for joining us. his article is very helpful
ReplyDeleteObat Keputihan Alami
Cara Mengobati Hematuria
Obat Penambah Berat Badan
Cara Mengobati Penyakit Kista
Cara Mengobati Sesak Napas Alami
Cara Mengobati Penyakit Gagal Jantung
Continue to Perbiki his blog to get a good blog !!
ReplyDeletePengobatan Penyakit Sinusitis
Pengobatan Varises dengan Bahan Alami
Obat Radang Dinding Rahim
Obat Penyakit Miom dan Kista
Obat Penyakit Sembelit
Cara Mengobati Mata Merah Berlendir
Cara Mengobati Penyakit Kencing Nanah
This article is interesting and useful. Thank you for sharing. And let me share an article about health that God willing will be very useful. Thank you :)
ReplyDeletePengobatan Scabies secara Alami
Obat Penghilang Jerawat dan Bekasnya
Cara Menyembuhkan Batuk Berkepanjangan
Cara Mengobati Lipoma secara Alami
Obat ISPA paling Ampuh
Obat Herpes Zoster
https://myserviceshome.com/detection-of-water-leaks-in-jazan/
ReplyDeletehttps://myserviceshome.com/fight-against-insects-in-hail/
https://myserviceshome.com/cleaning-houses-in-hail/
https://myserviceshome.com/moving-furniture-in-hail/
https://myserviceshome.com/detection-of-water-leaks-in-hail/
Sharing nih bro Obat penghilang bercak putih pada kulit seperti panu noah Obat eksim paling ampuh after Obat prostat bengkak fast Obat Turun Berok steady Obat jamur di kulit kepala just Obat lipoma tanpa operasi also Obat penumbuh jaringan kulit dan daging write Obat benjolan di depan telinga kanan dan kiri going Obat luka usus worry Obat limpa bengkak Thank you so much...
ReplyDeleteNice Article ;)
ReplyDeletePengobatan Atasi Penyakit Tiroiditis
Tips Mengobati Infeksi Rahim Secara Alami
Cara Mengobati Infeksi Kulit
Pengobatan Atasi Saraf Mata Rusak
Cara Alami Mengobati Ruam Di Kulit
Cara Mengobati Benjolan Di Leher
Thank you, the article is very petrifying
ReplyDeleteTips Untuk Menormalkan Kanker Kelenjar Getah Bening
Obat Herbal Radang Selaput Dada
Cara Mengobati Anyang-anyangan
هل تبحث عن شركة متخصصة فى خدمات التنظيف بالطائف بافضل المعدات والسوائل وثقة تمة فى العمل ودقة فى النتائج كل هذه المميزت توفرها شركة الخليج الشركة الافضل والامثل فى الخدمات المنزلية بالطائف وبما اننا الشركة الافضل والامثل بدون منافس سوف نسعى لتوفر افضل الخدمات باقل تكلفة وبقدر كبير من الاهتمام والدقة عزيزى اينما كنت فى اى منطقة ا وحى تابع لمدينة الطائف اتصل بنا وسوف نصلك فى الحال شركة الخليج للخدمات المنزلية شركة تنظيف منازل بالطائف
ReplyDeleteشركة تنظيف فلل بالطائف
شركة تنظيف خزانات بالطائف
شركة تسليك مجارى بالطائف
شركة رش مبيدات بالطائف
شركة مكافحة نمل ابيض بالطائف
شركة مكافحة حشرات بالطائف
شركة عزل اسطح بالطائف
شركة عزل خزانات بالطائف
تعمل شركة اللمسه في مكافحة الحشرات مثل النمل والصراصير وجميع انواع الحشرات بأبها الان ، وتعتبر شركة اللمسه من اشهر شركات مكافحة الحشرات في السعودية , لدينا عمالة مدربة ومتخصصة المواد المستخدمة فى مكافحة الحشرات غير ضارة على الانسان وليس لها روائح نعمل بدون مغادرة المنزل نعمل على راحة عملائنا .
ReplyDeleteشركة مكافحة النمل الابيض بنجران
شركة رش مبيدات بالخرج
شركة مكافحة حشرات بالخرج
شركة مكافحة النمل الابيض بالخرج
شركة رش مبيدات بأبها
شركة مكافحة حشرات بأبها
شركة مكافحة النمل الابيض بأبها
شركة رش مبيدات بنجران
شركة مكافحة حشرات بنجران
شركة رش مبيدات بالعينه
cheap nfl jerseys
ReplyDeletecheap jerseys
cheap jerseys from china
wholesale jerseys
cheap nfl jerseys from china
china jerseys
nfl jerseys china
wholesale nfl jerseys
cheap authentic nfl jerseys
cheap jerseys online
cheap authentic jerseys
cheap sports jerseys
cheap wholesale jerseys
china wholesale jerseys
discount nfl jerseys
cheap authentic jerseys from china
discount jerseys
custom cowboys jersey
nfl jerseys cheap
cheap nfl jerseys china
authentic nfl jerseys
camisetas de futbol
ReplyDeletecamisetas de futbol baratas
camisetas futbol baratas
camiseta real madrid barata
equipaciones de futbol baratas
comprar camisetas de futbol
camisetas de futbol baratas 2017
comprar camisetas de futbol
tiendas de futbol
camisetas de futbol
camisetas futbol baratas
camisetas futbol
camiseta real madrid barata
bounty camisetas futbol
camisetas de futbol 2018
maglie calcio a poco prezzo
ReplyDeletemaglie calcio poco prezzo
maglie calcio 2018
maglie italia
maglie calcio a poco prezzo
maglie calcio poco prezzo
maglie calcio 2018
maglie italia
maglie calcio a poco prezzo
maglie calcio poco prezzo
maglie calcio 2018
maglie italia
maglie calcio a poco prezzo
maglie calcio poco prezzo
maglie calcio 2018
maglie italia
maglie calcio a poco prezzo 2018
maglie calcio poco prezzo 2018
camisetas de futbol
ReplyDeletecamisetas de futbol baratas
camisetas futbol baratas
camiseta real madrid barata
equipaciones de futbol baratas
comprar camisetas de futbol
camisetas de futbol baratas 2017
comprar camisetas de futbol
tiendas de futbol
camisetas de futbol
camisetas futbol baratas
camisetas futbol
camiseta real madrid barata
bounty camisetas futbol
camisetas de futbol 2018
Thank you for this wonderful and useful article
ReplyDeleteشركة مكافحة حشرات بخميس مشيط
شركة مكافحة حشرات بالقصيم
شركة مكافحة حشرات بأبها
شركة مكافحة حشرات بنجران
شركة مكافحة حشرات بجازان
شركة مكافحة حشرات ببريدة
شركة مكافحة حشرات بالطائف
شركة مكافحة حشرات بالطائف
ReplyDeleteشركة مكافحة حشرات بالرياض
شركة مكافحة حشرات بخميس مشيط
ReplyDeleteشركة مكافحة حشرات ببيشة
Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agian
ReplyDeleteBirth certificate in delhi
Birth certificate in ghaziabad
Birth certificate in gurgaon
Birth certificate in noida
How to get birth certificate in ghaziabad
how to get birth certificate in delhi
birth certificate agent in delhi
how to download birth certificate
birth certificate in greater noida
birth certificate agent in delhi
Birth certificate delhi
Good post.Marketing Sweet
ReplyDelete
ReplyDeletegeek squad appointment |
best buy geek squad appointment |
best buy appointment |
geek squad appointment scheduling |
best buy geek squad appointment schedule |
bestbuy.com-appointments |