29 Mar 2017

PowerShell: Remove Site Collection Administrator Account

This time, I’m going to share you on the script to remove Site Collection Administrator of ALL site collections at one go.

12_13_47-000117


[Cmdletbinding()]
param (
    [string]$UserName = "",
    [string]$Tenant = "",
	[Parameter(Mandatory=$true)]
    [string]$AccountToRemove = "",
    [string]$RelativeSiteUrl = ""
)

Connect-SPOService -Url "https://$($Tenant)-admin.sharepoint.com" -Credential $UserName

if ($RelativeSiteUrl)
{
    $siteUrl = "https://$($Tenant).sharepoint.com$($RelativeSiteUrl)"
    Write-Host "Removing $AccountToRemove from $siteUrl..." -Fore Yellow -NoNewline
    Set-SPOUser -Site $site.Url -LoginName $AccountToRemove -IsSiteCollectionAdmin $false
    Write-Host "REMOVED" -Fore Green
}
else
{
    $SiteColl = Get-SPOSite
    Write-Host "Removing $AccountToRemove from:" -Fore Green
    foreach ($site in $SiteColl)
    {
        Write-Host "`t$($site.Url)..." -Fore Yellow -NoNewline
        Set-SPOUser -Site $site.Url -LoginName $AccountToRemove -IsSiteCollectionAdmin $false
        Write-Host "REMOVED" -Fore Green
    }
}

Disconnect-SPOService

Enjoy!

22 Mar 2017

PowerShell Script to Iterate All Documents (including sub-folders)

Seems like I want to dump again an old script to you. This PowerShell Script is commonly used to list down all documents in the Document Library regardless of their location, whether it’s in the root or sub-folders.

No need to explain in detail again, here’s the script.

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"
$Siteurl = "https://consotoayam.com/sites/EPRG" #you can replace this url with your own
$ListName = "Shared Documents" #replace this with your own Document Library name
$credential = Get-Credential #to prompt for credential

if ($SiteUrl -ne $null)
{
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    if ($ctx -ne $null)
    {
        $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)

        $st = $ctx.Site
        $wb = $ctx.Web
        $DocsLib = $wb.Lists.GetByTitle($ListName)

        $q = New-Object Microsoft.SharePoint.Client.CamlQuery
        $q.ViewXml = '0'

        $items = $DocsLib.GetItems($q)
        $ctx.Load($st)
        $ctx.Load($wb)
        $ctx.Load($DocsLib)
        $ctx.Load($items)
        $ctx.ExecuteQuery()
        foreach($item in $items)
        {
	    #if necessary, you can load the item
            $ctx.Load($item)
            $ctx.ExecuteQuery()
        }
    }
}

Enjoy!