Tuesday, 8 September 2015

SharePoint 2013 Powershell Script for getting all Site collection Users

I had to find all the users of a site collection and write to a file,  to add to another site. I was looking for one but couldn't find any, this script will dump all the users into a file with a semicolumn so it will be a straight copy paste.


Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) > $null
$SiteCollectionURL = "http://You site/collection name"
$site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL)
$web = $site.openweb()
$siteUsers = $web.SiteUsers

foreach($user in $siteUsers)

Write-Host "Site Collection URL:", $SiteCollectionURL
if($user.IsSiteAdmin -eq $true)
Write-Host "ADMIN: ", $user.LoginName 

  <# This will append all users on to a Text file 
  Add-Content c:\userlist.txt  $user.LoginName -Append #>

  <# This will append all users on to a Text file with no newline
  $user.LoginName | Out-File c:\userlist.txt   -nonewline #>

  <# Another way to output the file
  Write-output $user.LoginName  | Out-File c:\myfile.txt -Append #>

  <# This will add all the user on a Single line with a semicolumn so that you can directly reuse this with no formatting #>

  $alluserlist +=  $user.LoginName + ";"

<# Showing the set of userd and writing the users to a txt file #>

  Write-Host $alluserlist
  [system.io.file]::WriteAllText("C:\io.txt", $alluserlist)

SharePoint Template Sites

  SharePoint look book Get inspired Discover the modern experiences you can build with SharePoint in Microsoft 365 https://lookbook.microsof...