Introduction: Consider a scenario where you want to remove multiple users or groups from SharePoint web applications. Doing it manually consumes time. A PowerShell script is useful here. This articles outlines how to remove users or groups from a SharePoint web app user policy using a PowerShell script.

Functionality: The script discussed in this section does the following:

1. Remove a specific user or group from the web app policy
2. Remove a list of users or groups from the web app policy

Function 1
The following piece of code removes a specific user of a group from the web app policy, this functionality requires an input file (WebapplicationList.txt) that lists the web application details in which the user or group should be removed.
  1. Function RemoveSpecificUser()
  2. {
  3. $UserOrGroup = read-host "Enter the user or group to remove from the user policy (e.g domain\user) "
  4. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  5. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  6. if($Didyouplacethefile -eq 'y')
  7. {
  8. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  9. if($testpath)
  10. {
  11. foreach($webapplication in get-content"$scriptbase\WebapplicationList.txt")
  12. {
  13. $webapp = get-spwebapplication $webapplication -ea silentlycontinue
  14. if($webapp -ne $null)
  15. {
  16. #Not enumerating the policies... Once the policy changes by removing the first user it terminates enumeration at that step, so sending it to an output file
  17. foreach($policy in $webapp.policies)
  18. {
  19. $policy.username | out-file $scriptbase\UserName.txt -append
  20. }
  21. foreach($username in get-content "$scriptbase\UserName.txt")
  22. {
  23. if($username -eq $UserOrGroup)
  24. {
  25. write-host "User policy found" -for magenta
  26. write-host "Removing user policy for the user or group " $userorgroup " from the webapplication " $webapplication -fore yellow
  27. $policy = $webApp.Policies.Remove($userOrGroup)
  28. write-host "User policy for the User or group " $userorgroup " removed from the webapplication " $webapplication -fore green
  29. $webApp.Update()
  30. }
  31. else
  32. {
  33. write-host "No action required for the user or group " $username " on the web app " $webapplication -fore cyan
  34. }
  35. }
  36. #removing the output file for next web app in the list
  37. remove-item $scriptbase\UserName.txt -confirm:$false
  38. write-host ""
  39. write-host ""
  40. }
  41. else
  42. {
  43. write-host ""
  44. write-host "Invalid webapplication ...." $webapplication " please check the URL ...." -fore red
  45. write-host ""
  46. }
  47. }
  48. }
  49. else
  50. {
  51. write-host ""
  52. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  53. write-host ""
  54. }
  55. }
  56. else
  57. {
  58. write-host ""
  59. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  60. write-host ""
  61. }
  62. }

Function 2
The following piece of code removes multiple users or groups from a web app policy. This functionality requires 2 input files (WebapplicationList.txt and UserList.txt).

• WebapplicationList.txt: to hold the list of web applications in which the users or groups are to be removed
• UserList.txt: to hold the list of users or groups that needs to be removed from the web applications
  1. Function RemoveListOfUsers()
  2. {
  3. write-host "Place the UserList.txt file under the folder where the script exists" -fore Magenta
  4. $Didyouplacethefile = read-host "Did you place the UserList.txt file under the folder where the script exists (y/n)?"
  5. if($Didyouplacethefile -eq 'y')
  6. {
  7. $TestPath = Test-path -path $scriptbase\UserList.txt
  8. if($TestPath)
  9. {
  10. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  11. $Didyouplacethefile1 = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  12. if($Didyouplacethefile1 -eq 'y')
  13. {
  14. $TestPath1 = Test-path -path $scriptbase\WebapplicationList.txt
  15. if($TestPath1)
  16. {
  17. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  18. {
  19. $webapp = get-spwebapplication $webapplication -ea silentlycontinue
  20. if($webapp -ne $null)
  21. {
  22. foreach($user in get-content $scriptbase\UserList.txt)
  23. {
  24. #Not enumerating the policies... Once the policy changes by removing the first user it terminates enumeration at that step, so sending it to an output file
  25. foreach($policy in $webapp.policies)
  26. {
  27. $policy.username | out-file $scriptbase\UserName.txt -append
  28. }
  29. foreach($username in get-content "$scriptbase\UserName.txt")
  30. {
  31. if($username -eq $user)
  32. {
  33. write-host "User policy found" -fore magenta
  34. write-host "Removing user policy for the user or group " $user " from the webapplication " $webapplication -fore yellow
  35. $policy = $webApp.Policies.Remove($user)
  36. write-host "User policy for the User or group " $user " removed from the webapplication " $webapplication -fore green
  37. $webApp.Update()
  38. }
  39. }
  40. #removing the output file for next web app in the list
  41. remove-item $scriptbase\UserName.txt -confirm:$false
  42. write-host ""
  43. write-host ""
  44. }
  45. }
  46. else
  47. {
  48. write-host ""
  49. write-host "Invalid webapplication ...." $webapplication " please check the URL ...." -fore red
  50. write-host ""
  51. }
  52. }
  53. }
  54. else
  55. {
  56. write-host ""
  57. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  58. write-host ""
  59. }
  60. }
  61. else
  62. {
  63. write-host ""
  64. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  65. write-host ""
  66. }
  67. }
  68. else
  69. {
  70. write-host ""
  71. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  72. write-host ""
  73. }
  74. }
  75. else
  76. {
  77. write-host ""
  78. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  79. write-host ""
  80. }
  81. }
  82. Complete Code:
  83. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
  84. $LogFile = ".\RemoveUserOrGroupFromWebAppPolicyPatch-$LogTime.rtf"
  85. # Add SharePoint PowerShell Snapin
  86. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
  87. {
  88. Add-PSSnapin Microsoft.SharePoint.Powershell
  89. }
  90. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  91. Set-Location $scriptBase
  92. write-host "TESTING FOR LOG FOLDER EXISTENCE" -fore yellow
  93. $TestLogFolder = test-path -path $scriptbase\Logs
  94. if($TestLogFolder)
  95. {
  96. write-host "The log folder already exist in the script location" -fore yellow
  97. $clearlogfolder = read-host "Do you want to clear the log folder (y/n)"
  98. if($clearlogfolder -eq 'y')
  99. {
  100. write-host "The user choosen to clear the log folder" -fore yellow
  101. write-host "Clearing the log folder" -fore yellow
  102. remove-item $scriptbase\Logs\* -recurse -confirm:$false
  103. write-host "Log folder cleared" -fore yellow
  104. }
  105. else
  106. {
  107. write-host "The user choosen not to clear the log files" -fore yellow
  108. }
  109. }
  110. else
  111. {
  112. write-host "Log folder does not exist" -fore yellow
  113. write-host "Creating a log folder" -fore yellow
  114. New-Item $Scriptbase\Logs -type directory
  115. write-host "Log folder created" -fore yellow
  116. }
  117. #moving any .rtf files in the scriptbase location
  118. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  119. if($FindRTFFile)
  120. {
  121. write-host "Some old log files are found in the script location" -fore yellow
  122. write-host "Moving old log files into the Logs folder" -fore yellow
  123. foreach($file in $FindRTFFile)
  124. {
  125. move-item -path $file -destination $scriptbase\logs
  126. }
  127. write-host "Old log files moved successfully" -fore yellow
  128. }
  129. start-transcript $logfile
  130. Function RemoveSpecificUser()
  131. {
  132. $UserOrGroup = read-host "Enter the user or group to remove from the user policy (e.g domain\user) "
  133. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  134. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  135. if($Didyouplacethefile -eq 'y')
  136. {
  137. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  138. if($testpath)
  139. {
  140. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  141. {
  142. $webapp = get-spwebapplication $webapplication -ea silentlycontinue
  143. if($webapp -ne $null)
  144. {
  145. #Not enumerating the policies... Once the policy changes by removing the first user it terminates enumeration at that step, so sending it to an output file
  146. foreach($policy in $webapp.policies)
  147. {
  148. $policy.username | out-file $scriptbase\UserName.txt -append
  149. }
  150. foreach($username in get-content "$scriptbase\UserName.txt")
  151. {
  152. if($username -eq $UserOrGroup)
  153. {
  154. write-host "User policy found" -for magenta
  155. write-host "Removing user policy for the user or group " $userorgroup " from the webapplication " $webapplication -fore yellow
  156. $policy = $webApp.Policies.Remove($userOrGroup)
  157. write-host "User policy for the User or group " $userorgroup " removed from the webapplication " $webapplication -fore green
  158. $webApp.Update()
  159. }
  160. else
  161. {
  162. write-host "No action required for the user or group " $username " on the web app " $webapplication -fore cyan
  163. }
  164. }
  165. #removing the output file for next web app in the list
  166. remove-item $scriptbase\UserName.txt -confirm:$false
  167. write-host ""
  168. write-host ""
  169. }
  170. else
  171. {
  172. write-host ""
  173. write-host "Invalid webapplication ...." $webapplication " please check the URL ...." -fore red
  174. write-host ""
  175. }
  176. }
  177. }
  178. else
  179. {
  180. write-host ""
  181. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  182. write-host ""
  183. }
  184. }
  185. else
  186. {
  187. write-host ""
  188. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  189. write-host ""
  190. }
  191. }
  192. Function RemoveListOfUsers()
  193. {
  194. write-host "Place the UserList.txt file under the folder where the script exists" -fore Magenta
  195. $Didyouplacethefile = read-host "Did you place the UserList.txt file under the folder where the script exists (y/n)?"
  196. if($Didyouplacethefile -eq 'y')
  197. {
  198. $TestPath = Test-path -path $scriptbase\UserList.txt
  199. if($TestPath)
  200. {
  201. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  202. $Didyouplacethefile1 = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  203. if($Didyouplacethefile1 -eq 'y')
  204. {
  205. $TestPath1 = Test-path -path $scriptbase\WebapplicationList.txt
  206. if($TestPath1)
  207. {
  208. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  209. {
  210. $webapp = get-spwebapplication $webapplication -ea silentlycontinue
  211. if($webapp -ne $null)
  212. {
  213. foreach($user in get-content $scriptbase\UserList.txt)
  214. {
  215. #Not enumerating the policies... Once the policy changes by removing the first user it terminates enumeration at that step, so sending it to an output file
  216. foreach($policy in $webapp.policies)
  217. {
  218. $policy.username | out-file $scriptbase\UserName.txt -append
  219. }
  220. foreach($username in get-content "$scriptbase\UserName.txt")
  221. {
  222. if($username -eq $user)
  223. {
  224. write-host "User policy found" -fore magenta
  225. write-host "Removing user policy for the user or group " $user " from the webapplication " $webapplication -fore yellow
  226. $policy = $webApp.Policies.Remove($user)
  227. write-host "User policy for the User or group " $user " removed from the webapplication " $webapplication -fore green
  228. $webApp.Update()
  229. }
  230. }
  231. #removing the output file for next web app in the list
  232. remove-item $scriptbase\UserName.txt -confirm:$false
  233. write-host ""
  234. write-host ""
  235. }
  236. }
  237. else
  238. {
  239. write-host ""
  240. write-host "Invalid webapplication ...." $webapplication " please check the URL ...." -fore red
  241. write-host ""
  242. }
  243. }
  244. }
  245. else
  246. {
  247. write-host ""
  248. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  249. write-host ""
  250. }
  251. }
  252. else
  253. {
  254. write-host ""
  255. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  256. write-host ""
  257. }
  258. }
  259. else
  260. {
  261. write-host ""
  262. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  263. write-host ""
  264. }
  265. }
  266. else
  267. {
  268. write-host ""
  269. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  270. write-host ""
  271. }
  272. }
  273. write-host "########################################################################################################" -fore cyan
  274. write-host "Enter 1 to remove specific user from user policy" -fore green
  275. write-host "Enter 2 to remove list of users from user policy" -fore green
  276. write-host "########################################################################################################" -fore cyan
  277. $option = read-host "Enter the option "
  278. switch($option)
  279. {
  280. 1{
  281. RemoveSpecificUser
  282. }
  283. 2{
  284. RemoveListOfUsers
  285. }
  286. }
  287. write-host "SCRIPT COMPLETED" -fore Blue
  288. stop-transcript
Execution Procedure

Step 1: Download and copy the script to the SharePoint server.
Step 2: Launch the SharePoint management shell.
Step 3: Navigate to the script path and execute the following script:
Enter option “1” or “2” to get the desired output.

Conclusion

Thus this article outlined how to remove users or groups from a SharePoint web app policy using a PowerShell script.