Remove Legacy User groups at Site collection level:-
Input :- Two input files (CSV format) are used in this script.
1st input file (Asset.CSV) will have all site collection url listed one below the other.
2nd input file (Group.CSV) will have legacy SharePoint user group names listed one below the other.
Output:-
Legacy groups will be removed with a log generated with same info.
Script Source:-
Add-PSSnapin Microsoft.SharePoint.PowerShell
$global:strLoanListFileName = Read-Host "Enter the location of Asset.csv"
$global:strGroupListFileName = Read-Host "Enter the location of Groups.csv"
$LogPath = Read-Host "Enter a location for log file :"
$LogFileName = Read-Host "Enter name for log file :"
$FilePath = $LogPath + "\" + $LogFileName
function write-log([string]$label, [string]$logMsg)
{
if($logFileCreated -eq $False)
{
write-host "Creating log file..."
if((Test-Path -path $LogPath) -ne $True)
{
write-host "Provide proper values to LogPath folder" -ForegroundColor Red
}
else
{
Add-Content -Path $FilePath -Value $logHeader
$script:logFileCreated = $True
write-host "Log file created..."
}
}
else
{
[string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
Add-Content -Path $FilePath -Value $info
}
}
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
Write-Host "Deletion Process Started...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
write-log "Deletion Process Started...Time -" "$(get-date -format HH:mm:ss)"
$StartTime=Get-Date
Import-CSV -Path $global:strLoanListFileName | ForEach-Object {
if($_.AssetID -ne $null -and $_.AssetID -ne "")
{
$global:LoanNo=$_.AssetID
Write-Host $global:LoanNo
write-log $global:LoanNo
Import-CSV -Path $global:strGroupListFileName | ForEach-Object {
if($_.Group -ne $null -and $_.Group -ne "")
{
$global:Group=$_.Group
write-log "Site Group"
write-log $global:Group
deleteGroup
}
}
}
}
$StartTime=(Get-Date) - $StartTime
write-host "Total execution time- " $StartTime
write-log "Total execution time- " $StartTime
Write-Host "Deletion Process Completed...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
write-log "Deletion Process Completed...Time -" "$(get-date -format HH:mm:ss)"
})
function deleteGroup
{
$web = get-SPWeb $global:LoanNo
$GroupName =$global:Group
$objSiteGroup = $web.SiteGroups["$GroupName"]
if ($objSiteGroup)
{
$web.SiteGroups.Remove(“$GroupName”)
write-log "Group Deleted"
$web.Update()
}
else
{
write-host " Group does not exist.."
write-log " Group does not exist.."
}
$web.Dispose()
}
Input :- Two input files (CSV format) are used in this script.
1st input file (Asset.CSV) will have all site collection url listed one below the other.
2nd input file (Group.CSV) will have legacy SharePoint user group names listed one below the other.
Output:-
Legacy groups will be removed with a log generated with same info.
Script Source:-
Add-PSSnapin Microsoft.SharePoint.PowerShell
$global:strLoanListFileName = Read-Host "Enter the location of Asset.csv"
$global:strGroupListFileName = Read-Host "Enter the location of Groups.csv"
$LogPath = Read-Host "Enter a location for log file :"
$LogFileName = Read-Host "Enter name for log file :"
$FilePath = $LogPath + "\" + $LogFileName
function write-log([string]$label, [string]$logMsg)
{
if($logFileCreated -eq $False)
{
write-host "Creating log file..."
if((Test-Path -path $LogPath) -ne $True)
{
write-host "Provide proper values to LogPath folder" -ForegroundColor Red
}
else
{
Add-Content -Path $FilePath -Value $logHeader
$script:logFileCreated = $True
write-host "Log file created..."
}
}
else
{
[string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
Add-Content -Path $FilePath -Value $info
}
}
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
Write-Host "Deletion Process Started...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
write-log "Deletion Process Started...Time -" "$(get-date -format HH:mm:ss)"
$StartTime=Get-Date
Import-CSV -Path $global:strLoanListFileName | ForEach-Object {
if($_.AssetID -ne $null -and $_.AssetID -ne "")
{
$global:LoanNo=$_.AssetID
Write-Host $global:LoanNo
write-log $global:LoanNo
Import-CSV -Path $global:strGroupListFileName | ForEach-Object {
if($_.Group -ne $null -and $_.Group -ne "")
{
$global:Group=$_.Group
write-log "Site Group"
write-log $global:Group
deleteGroup
}
}
}
}
$StartTime=(Get-Date) - $StartTime
write-host "Total execution time- " $StartTime
write-log "Total execution time- " $StartTime
Write-Host "Deletion Process Completed...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
write-log "Deletion Process Completed...Time -" "$(get-date -format HH:mm:ss)"
})
function deleteGroup
{
$web = get-SPWeb $global:LoanNo
$GroupName =$global:Group
$objSiteGroup = $web.SiteGroups["$GroupName"]
if ($objSiteGroup)
{
$web.SiteGroups.Remove(“$GroupName”)
write-log "Group Deleted"
$web.Update()
}
else
{
write-host " Group does not exist.."
write-log " Group does not exist.."
}
$web.Dispose()
}