Script to remove users from local server administrator group. This script requires "RemoveUsers.csv" input file which has the list of users that needs to be removed from the local server admin group.
- if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
- Add-PSSnapin Microsoft.SharePoint.Powershell
- }
- $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
- Set-Location $scriptBase
-
- Function RemoveUserFromServerAdminGroup([String]$AdminMember, [String]$ServerName)
- {
-
- $ans = read-host "Do you want to remove user $AdminMember from server $ServerName (y/n)? "
- if($ans -eq 'y')
- {
- write-host "Removing user " $AdminMember " from administrator group on server " $ServerName -fore yellow
-
- $AdminMember1 = $AdminMember.split("\")
- $AdminMember2 = $AdminMember1[0] + "/" + $AdminMember1[1]
-
- $GroupObj = [ADSI]"WinNT://$ServerName/Administrators"
- $GroupObj.Remove("WinNT://$AdminMember2")
- write-host $AdminMember " removed from the local administrator group on the server " $ServerName -fore green
- }
- else
- {
- write-host "User choose not to remove user " $AdminMember " from the server " $ServerName " administrator group" -fore cyan
- }
-
- }
-
- write-host "Preparing to remove users from the server administrator group" -fore magenta
- $csvfile1 = $scriptbase + "\" + "RemoveUsers.csv"
- import-csv $csvfile1 | where {
- RemoveUserFromServerAdminGroup $_.AdminMember $_.ServerName
- }
- write-host "Users has been removed from local administrators group" -fore green
-
- RemoveUserFromServerAdminGroup