Friday, 4 July 2014

PowerShell : List all site level users with User Groups name

PowerShell : List all site level users with User Groups name:-
Input:-
1. Pass site URL as input
2. Pass output file path with file name and extension as "CSV"

Code:-
$destWebappUrl= Read-Host "Enter site URL (Eg: http://<HostName>:8080/ )"
$LogFilePath =  Read-Host "Enter Log file path (C:\.....\Log.csv)"

# =========================== LOADING SHAREPOITN POWERSHELL SNAPIN FILE DETAILS =====================================
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
                Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
                $snapin = Add-PSSnapin "Microsoft.Sharepoint.Powershell"
                Write-Host $snapin
}

$site = Get-SPSite $destWebappUrl

Function GetUsersFromUserGroups
{
    $groups = $site.RootWeb.sitegroups
    foreach ($grp in $groups)
    {
        $groupName= $grp.name;
        foreach ($user in $grp.users)
        {
            $userName= $user.name
            $msg = "$groupName,$userName"  |  Out-File $LogFilePath -append
        }
    }
    $site.Dispose()
}

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
    Write-Host "Process Started...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Yellow
 
    $StartTime=Get-Date
    $msg = "User Group Name, User Name"  |  Out-File $LogFilePath -append          
    Write-Host "Processing..."
    GetUsersFromUserGroups
 
    $StartTime=(Get-Date) - $StartTime
    write-host "Total execution time- " $StartTime
    Write-Host "Process Completed...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Yellow

})

No comments:

Post a Comment