Search This Blog

Thursday, March 29, 2018

Find AD-DomainGroup for List/Document in SharePoint with PowerShell


step : Save below script .Ps1 and run with powershell
step 2:Save .bat file under script and run with administrator 

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
#Change to your web application
$WebAppURL = "http://win-2016"

#Get Web Application
$WebApp = Get-SPWebApplication $WebAppURL

#variable for data collection
$ADGroupCollection= @()
$ReportPath ="C:\ADGroups.csv"

foreach ($Site in $WebApp.Sites)
{
    Write-host -foregroundcolor green "Processing Site Collection: "$site.RootWeb.URL
      foreach($web in $Site.AllWebs)
        {
 
     foreach($List in $Web.lists)
              {
                #Get all AD Security Groups from the site collection
    $ADGroups = Get-SPUser -Web $List.ParentWeb.Url | Where { $_.IsDomainGroup -and $_.displayName -ne "Everyone" }

     $url = $List.ParentWeb.Url+"/"+$List.Title
    #Iterate through each AD Group
    foreach($Group in $ADGroups)
                    {
            Write-host "Found AD Group:" $Group.DisplayName

            #Get Direct Permissions
            $Permissions = $Group.Roles | Where { $_.Name -ne "Limited Access" } | Select -ExpandProperty Name

            #Get SharePoint User Groups where the AD group is member of.
            $SiteGroups = $Group.Groups | Select -ExpandProperty Name

            #Send Data to an object array
            $ADGroup = new-object psobject
            $ADGroup | add-member noteproperty -name "Site Collection" -value $Site.RootWeb.Url
            $ADGroup | add-member noteproperty -name "URL" -value $url
            $ADGroup | add-member noteproperty -name "Group Name" -value $Group.DisplayName
            $ADGroup | add-member noteproperty -name "Direct Permissions" -value ($Permissions -join ",")
            $ADGroup | add-member noteproperty -name "SharePoint Groups" -value ($SiteGroups -join ",")
            #Add to Array
            $ADGroupCollection+=$ADGroup         
                     }
                }
        }
}
    #Export Data to CSV
    $ADGroupCollection | export-csv $ReportPath -notypeinformation
    Write-host "SharePoint Security Groups data exported to a CSV file at:"$ReportPath -ForegroundColor Cyan

-----------------------------AdGroup.bat-----------------------------------
cd /d %~dp0
powershell -noexit -file "AdGroupReport.PS1" "%CD%"
pause


Monday, March 5, 2018

SharePoint - Document List Item Export with CSV - PowerShell


if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

#Get the Web
$web = Get-SPWeb -identity "http://win-2016"

#Get the Target List
$list = $web.Lists["Documents"]

#Array to Hold Result - PSObjects
$ListItemCollection = @()

#CAML Query
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = 2000
$caml="<OrderBy><FieldRef Name='ID' Ascending='True' /></OrderBy>"
$spQuery.Query = $caml

#Get All List items
$listItems=$list.GetItems($spQuery)
foreach($item in $listItems) {
$data = @{
         
                        "URL"=$web.Site.MakeFullUrl("$($web.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
        }
      
       $ExportItem = New-Object PSObject -Property $data | Select "URL"
       $ExportItem | Add-Member -MemberType NoteProperty -name "Name" -value $item["Name"]     
      
       #Add the object with property to an Array
       $ListItemCollection += $ExportItem
}
#Export the result Array to CSV file
$ListItemCollection | Export-CSV "c:\ListData.csv" -NoTypeInformation                      

#Dispose the web Object
$web.Dispose()


Exporting file'path' from drive - PowerShell


Save .PS1 file below script and runwith powershell.
----------------------------------------------------

#exporting file'path' from drive >path exist limit

get-childitem 'D:\TestingFolder' -recurse -force | where-object {
$_.FullName.Length -ge 256 } | select-object FullName | export-csv -notypeinformation -path Drivefiles.txt | % {$_.Replace('"','')}


#exporting file'path' from drive

get-childitem 'D:\TestingFolder' -Recurse | where {
!$_.PSIsContainer} | select-object FullName,  @{N='FriendlySize';E={Get-FriendlySize -Bytes $_.Length}} |Sort-Object -Property FriendlySize |  export-csv -notypeinformation -path Drivefiles.csv | % {$_.Replace('"','')}


function Get-FriendlySize {
    param($Bytes)
    $sizes='Bytes,KB,MB,GB,TB,PB,EB,ZB' -split ','
    for($i=0; ($Bytes -ge 1kb) -and
        ($i -lt $sizes.Count); $i++) {$Bytes/=1kb}
    $N=2; if($i -eq 0) {$N=0}
    "{0:N$($N)} {1}" -f $Bytes, $sizes[$i]
}