Table of Contents

SharePoint Powershell Scripts

Get all users and groups

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
 
try
{
 
       $objSiteColl = Get-SPSite http://portal.local
       "Site Name`tGroup Name`tUser Name"
       foreach ($objWeb in $objSiteColl.AllWebs)
       {
              write-host -foregroundcolor yellow "`nWorking on web:" $objWeb.Title
              foreach ($objGrp in $objWeb.groups)
              {
                     $grpName = $objGrp.name
                     write-host "`nGroup Name: " $grpName  -foregroundcolor green
                     foreach ($objUser in $objGrp.users)
                     {
                            $objWeb.Title + "`t" + $objGrp.name +"`t"+ $objUser.name
                            write-host "Login-ID:: " $objUser.UserLogin  -foregroundcolor red
                     }
              }
       }
}
 
catch
{
       Write-Host "`nError:: $($_.Exception.Message)" -foregroundcolor red -BackgroundColor Yellow
}

Get all Groups in a Site Collection

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
 
{
            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
 
$SPFile = ".\GetGroupInformation.txt"
$SPSiteUrl = Read-Host "Enter the Url of the Site Collection "
$SPSite = Get-SPSite $SPSiteUrl -ErrorAction SilentlyContinue
 
if($SPSite -ne $null)
{
    $SPWebCollection = $SPSite.AllWebs
    foreach($SPWeb in $SPWebCollection)
    {
        Write-Output $SPWeb.Title "contains the following groups"| Out-File $SPFile -append
        Write-Output "========================"| Out-File $SPFile -append
        foreach($SPGroup in $SPWeb.Groups)
        {
            Write-Output $SPGroup.Name| Out-File $SPFile -append
        }
        Write-Output "========================"| Out-File $SPFile -append
    }
}
 
else
{
Write-Host "Requested Site Could Not be found" -ForegroundColor DarkRed
}