Hello Readers,
This article as the name states will be a PowerShell script to get the email ids of the all the users present in a site and its sub sites. We get this requirement for the migration purpose and also when our clients need the email ids of all the users in their site to send email to.
This script will help you to save us developers a lot of time. Initially I thought I will save my effort by searching for the script but didn’t have one so thought of writing it for my fellow developers too.
So here is the script let’s see it here.
- Copy the code below to a .ps1 file.
- Create a new text file named anything.
- Add a heading in that text file as Webs, underline you can paste as many url of the sites as you want their email ids of all the users.
- Change the location of the file in the script.
- Run the .ps1 file
- You will receive the resulted file in a text file named as AllUserEmails.
Script
- #get all users from Site level
- #Snapin
- Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
- #File Output Name Generator
- $filenameStart = "AllUserEmails"
- $logfile = ("{0}.xml" -f $filenamestart)
-
- out-file -FilePath $logfile
- $userDetailsArray = @()
- $finaluserDetailsArray = @()
- #Calling the list and Connecting the site
- $Webst = @()
- $userobjects = Import-Csv “Text file with Sites Location here”
- Write-Host $userobjects
- foreach($userobject in $userobjects)
- {
- $site = new-object Microsoft.SharePoint.SPSite($userobject.Webs)
- Write-Host $userobject.Webs -foregroundcolor green
- foreach($sites in $site)
- {
- Write-Host $site -foregroundcolor blue
- $web = $site.openweb()
- Write-Host $web -foregroundcolor blue
- $siteUsers = $web.SiteUsers
- Write-Host $subs -foregroundcolor white
- foreach ($userw in $siteUsers)
- {
- if ($userw -like "Domain\*")
- {
- write-host " " $userw -foregroundcolor white
- $userwLogin = $userw.LoginName
- $userDetailsArray =$userDetailsArray + $userwLogin
- }
- }
- $web.Dispose()
- }
- }
- #Tagging them to an array
- $userArray = $userDetailsArray | select -unique
- foreach ($value in $userArray)
- {
- $valueemail = $value.Split("\")
- $emailval = $valueemail[1]
- #Result to txt File
- "$emailval"+"@gmail.com"+";" | out-file -FilePath $logfile -append
- }
Keep Leaning.
Cheers !!