none
Powershell to add users from Office 365 to Sharepoint based on date they were added

    Question

  • So I am using the following powershell script to add users from office 365 into Sharepoint but it adds all users and it takes some time.  It would be best to add just users into Sharepoint that have been added into Office 365 in the last xx amount of days or so.

    #This powershell will add users to Sharepoint Group "Viewers" from Office 365

    connect-msolservice

    connect-sposervice

    -urlhttps://OURSITE

    $filename ="{0:yyyy_MM_dd}"-f(Get-Date)$msolusers =Get-MsolUser-ALL|Where-Object{ $_.IsLicensed -eq$true}ForEach ( $uin$msolusers) {add-spouser-sitehttps://OURSite-loginname$u.UserPrincipalName -group"viewers"}

    $msolusers |export-csvC:\O365_to_Sharepoint_$filename.csv

    Any way of putting a qualifier in there that just adds users added into Office 365 in the last xx amount of days?  Thanks....this online resources is incredibly helpful!!

    Friday, September 06, 2013 5:48 PM

Answers

  • Hi,

    As far as I know, there is no attribute used to identiry the created date for created O356 users, so we sould use get-mailbox to get the date of when the user was added and assigned with a license.

    $filename ="{0:yyyy_MM_dd}"-f(Get-Date)
    
    $msolusers =Get-MsolUser -ALL|Where-Object{ $_.IsLicensed -eq$true}|get-mailbox -identity $_.name |where {$_.whenmailboxcreated -gt "1/8/2013" }
    
    ForEach ( $u in $msolusers) 
    
    {add-spouser-site https://OURSite -loginname $u.UserPrincipalName -group"viewers"}
    
    $msolusers |export-csvC:\O365_to_Sharepoint_$filename.csv 
    

    Regards,

    Yan Li


    Cataleya Li
    TechNet Community Support

    Monday, September 30, 2013 5:45 AM
    Moderator