Thursday, December 15, 2016

O365 Powershell script to get SharePoint Sites Size

Add-PSSnapin microsoft.sharepoint.powershell

$SizeLog = "D:\temp\SPSiteSize.csv"

############################################################

$CurrentDate = Get-Date -format d

$WebApps = Get-SPWebApplication

foreach($WebApp in $WebApps)

{

$Sites = Get-SPSite -WebApplication $WebApp -Limit All

foreach($Site in $Sites)
{

$SizeInKB = $Site.Usage.Storage

$SizeInGB = $SizeInKB/1024/1024/1024

$SizeInGB = [math]::Round($SizeInGB,2)

$CSVOutput = $Site.RootWeb.Title + "," + $Site.URL + "," + $Site.ContentDatabase.Name + "," + $SizeInGB + "," + $CurrentDate

$CSVOutput | Out-File $SizeLog -Append

}

}

$Site.Dispose()

O365 Powershell script to Delete user from all Site Collection



Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function DeleteUserFromAllSites([string]$WebAppURL, [string]$UserAccount, [bool]$ScanOnly)
{
   
   #Get the web application
   $WebApp = Get-SPWebApplication $WebAppURL
   
   #Loop through each site collection
   foreach ($Site in $WebApp.Sites)
   {
    try
           {
      $ErrorActionPreference = "Stop"
      #Try to get the User
      $User = $Site.RootWeb.SiteUsers | Where-Object {$_.LoginName -eq $UserAccount}
     
      #If user account found
      if($User -ne $null)
      {
       if($ScanOnly -eq $true)
       {
        Write-Host "Found user on: $($site.Rootweb.URL)"
       }
       else
       {
        #Remove the User from site collection
        $Site.RootWeb.SiteUsers.Remove($UserAccount)
        Write-Host "User Deleted from: $($site.Rootweb.URL)"
       }
      }
   
     }
    catch
           {
      #Write error message on screen and to a LOG file
               write-host "Error Deleting user from site collection: $($site.rootweb.url)`n" $_.Exception.Message
      $_.Exception.Message >> "d:\error.log"
           }
          finally
          {
              $ErrorActionPreference = "Continue"
                                $site.Dispose()
          }
   }
}

#Call the function
DeleteUserFromAllSites "http://sharepoint.crescent.com" "global\davep" $true

O365 Powershell script to Delete user from User Profile

#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


$site = new-object Microsoft.SharePoint.SPSite("http://skvkfm-it01/");
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);

#Get UserProfileManager from the My Site Host Site context
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)  
$AllProfiles = $ProfileManager.GetEnumerator()

foreach($profile in $AllProfiles)
{
    $DisplayName = $profile.DisplayName
    $AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value

    #Do not delete setup (admin) account from user profiles. Please enter the account name below
    if($AccountName -ne "Domain\MySiteSVApp")
    {
        $ProfileManager.RemoveUserProfile($AccountName);
        write-host "Profile for account ", $AccountName, " has been deleted"
    }

}
write-host "Finished."
$site.Dispose() 

O365 Powershell script to Delete user from given site

#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


#Remove site users retireved from AD in notepad file, create RemovedUsers.txt to write the users

$url = "http://teamsite/sites/Test"

$CurrentDirectory =  Split-Path -parent $MyInvocation.MyCommand.Definition
$users= Get-content "$CurrentDirectory\users.txt"


foreach($user in $users)
{

Write-Host $user


Remove-SPUser -Identity $user -Web $Web -Confirm:$False


Add-Content RemovedUsers.txt $user

}

write-Host "Finished"

O365 Powershell script to Delete User from Entire Web Application

#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}



#Delete from entire site coll, create RemovedUsers.txt to write the users


#Get the web application

$WebApp = Get-SPWebApplication $WebAppURL

#Loop through each site collection

foreach ($Site in $WebApp.Sites)
{


$CurrentDirectory =  Split-Path -parent $MyInvocation.MyCommand.Definition
$users= Get-content "$CurrentDirectory\users.txt"

foreach($user in $users)
{

Write-Host $user

Remove-SPUser -Identity $user -Web $Web -Confirm:$False

Add-Content RemovedUsers.txt $user

}


}


write-Host "Finished"

O365 Powershell script to get SharePoint Web Groups and its Count

Add-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
 Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell
}


$username = "username@microsoft.com"
$password = "12345"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

Add-Type -Path "D:\Dlls\Microsoft.SharePoint.Client.dll"
Add-Type -Path "D:\Dlls\Microsoft.SharePoint.Client.Runtime.dll"
#Get the Client Context and Bind the Site Collection


function Get-SPGroups()
{

//Load your sites as input file
$getSites= Get-content "D:\Scripts\Sites.txt"

foreach($webUrl in $getSites)
{

try{

$context = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$context.Credentials = $credentials
$web = $context.Web;
$context.Load($web)
$context.ExecuteQuery()
Write "Url: $webUrl : $_" >>D:\Scripts\UrlProcessed.txt
}
catch{
Write "Error: $webUrl : $_" >>D:\Scripts\Error.txt -Append
continue;
}


$ListItemCollection = @()
$groups = $web.RoleAssignments.Groups
$context.Load($groups);
$context.ExecuteQuery()

foreach($grp in $groups) {


$data = @{
                        "Site URL" = $webUrl
                        "Site Title" = $web.Title
                        "Group Name" = $grp.Title
"Group Count" = $groups.Count                                      
}

New-Object PSObject -Property $data | Select "Site URL", "Site Title","Group Name", "Group Count"



}

}

$context="";



}

Get-SPGroups  | Export-Csv -NoTypeInformation -Path D:\Scripts\Sites1.csv

O365 Powershell script to get List Item Properties with Versions

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb -identity "Site URL"
$list = $web.Lists["ListName"]

$ListItemCollection = @()

foreach ($item in $list.Items) {
foreach($version in $item.Versions){
$data = @{
                        "Version" = $version.VersionLabel
                        "List Name" = $list.Title
                        "Created By" = $item["Author"]
                        "Created Date" = $item["Created"]
                        "Modified By" = $item["Editor"]
                        "Modified Date" = $item["Modified"]
                        "Item Name" = $item.File.Name                      
}
$ExportItem = New-Object PSObject -Property $data | Select "List Name", "Item Name", "Version", "Created By", "Created Date", "Modified By", "Modified Date"

$ListItemCollection += $ExportItem

}
}

$ListItemCollection | Export-CSV D:\List.csv -NoTypeInformation