Tuesday, 21 June 2016

Create Office 365 Group using Powershell

Office 365 Groups provide a platform for collaboration that enables teams to come together and establish a single team identity and a single set of permissions across different Office 365 apps including Outlook, OneDrive, OneNote, Skype for Business, Power BI and Dynamics CRM. In this article, I am going write powershell commands to create Office 365 Groups, add members and owners to an Office 365 Group.

Before proceed, first connect Exchange Online Powershell session by using the following commands.
$365Logon = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $365Logon -Authentication Basic -AllowRedirection
Import-PSSession $Session
We can use the PowerShell cmdlet New-UnifiedGroup to create a new Office 365 group. This cmdlet includes the following key parameters:

DisplayName – display name of the new group
Alias – Email alias of the group. If you omit the parameter, it will generate an alias by using display name.
AccessType – Privacy type of the group (Public or Private)
AutoSubscribeNewMembers – Add this parameter to auto subscribe new members to the group

Use the below command to create a new group with minimal parameters.
New-UnifiedGroup –DisplayName "Test O365 Group 1"
Create the group with key parameters.
New-UnifiedGroup –DisplayName "Test O365 Group 2" -Alias "TestO365Group2" -AccessType Public
Once we created the group, we can use Get-UnifiedGroup cmdlet to list all the available groups.

Add Members and Owners to Office 365 Group

We can use Add-UnifiedGroupLinks cmdlet to add members and owners to the group. This cmdlet includes the following key parameters:

Identity – Alias, Display name, or Email address of the group
Links – Alias, Display name, or Email address of the user being added
LinkType – Members, Owners, or Subscribers

Add an user as owner: To add an user as owner to the group, first we need the user as a member to the specified group.
Add-UnifiedGroupLinks –Identity "TestO365Group2" –LinkType Members –Links Morgan
Add-UnifiedGroupLinks –Identity "TestO365Group2" –LinkType Owners –Links Morgan
Add member:
Add-UnifiedGroupLinks –Identity "TestO365Group2" –LinkType Members  –Links AlexD
Add subscriber: A subscriber who receives updates by email can be added by changing the LinkType to "Subscribers"
Add-UnifiedGroupLinks –Identity "TestO365Group2" –LinkType Subscribers  –Links AlexD
The parameter Links accept multiple values, use the following syntax: value1,value2.... If the values contain spaces or otherwise require quotation marks, use the following syntax: "value1","value2",....

Add members to multiple office 365 groups:

$Groups = "group 01","group 02","group 03"
$Groups | ForEach-Object {
Add-UnifiedGroupLinks –Identity $_ –LinkType Members  –Links "Morgan" }

Import office 365 group members from a CSV File:

You can use the below powershell commands to add members to an office 365 group by importing users from csv file. Consider the csv file members.csv that includes the column member which holds the member identity in each row of the csv file.
Import-CSV "C:\members.csv" | ForEach-Object {
Add-UnifiedGroupLinks –Identity "TestO365Group2" –LinkType Members  –Links $_.member
}

Find members and owners of a group:

Once we added the members and owners, we can use Get-UnifiedGroupLinks cmdlet to get members or owners of a specific group. The below command lists all members of the given group.
Get-UnifiedGroupLinks –Identity "TestO365Group2" –LinkType Members
List owners of a group.
Get-UnifiedGroupLinks –Identity "TestO365Group2" –LinkType Owners

Friday, 17 June 2016

Set Storage Quota for Office 365 Group Site using PowerShell

As you know, Office 365 Group is nothing but a hidden site collection that are not visible in Site Collections view in Office 365 Admin portal. You can only access these site collections by using PowerShell or through URL (https://<tanentname>.sharepoint.com/sites/<group-name>/Shared documents”). Since you can't view the site in Office 365 Admin portal, the only way set storage size limit is using Powershell. In this article, I am going to write powershel script to set maximum storage size and storage warning level for Office 365 group site.

We can use the SharePoint Online Powershell cmdlet Set-SPOSite to set storage quota and storage warning size limit. Before proceed, run the following command to connect Sharepoint Online powershell module.
Connect-SPOService -Url https://<tanentname>-admin.sharepoint.com -Credential admin@o365domain.com
Now, run the following script to set storage quota and warning level.
$StorageQuota= 2048 # 2GB or 2048MB
$WarningLevel = 1800 # 1800MB

$siteUrl ="https://<tanentname>.sharepoint.com/sites/<group-name>"
Set-SPOSite -Identity $siteUrl -StorageQuota $StorageQuota -StorageQuotaWarningLevel $WarningLevel

Set Storage Quota for all Office 365 Groups Site:

To set the storage quota for all the Office 365 Groups, first, we need to get sharepoint site url for all the office 365 groups by using Exchange Online cmdlet Get-UnifiedGroup. The following powershell script update storage quota and warning level for all the office 365 groups. You need to replace your own Office 365 tenant name and admin credentials.
$userName ="admin@<tanentname>.onmicrosoft.com" 
$o365Cred = Get-Credential -UserName $userName -Message "Enter Office 365 Admin Credentials"
$o365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $o365Cred -Authentication Basic -AllowRedirection
Import-PSSession $o365Session

$spoAdminUrl ="https://<tanentname>-admin.sharepoint.com/" 
Connect-SPOService -Url $spoAdminUrl -Credential $o365Cred 

$O365Groups = Get-UnifiedGroup -ResultSize Unlimited

$StorageQuota= 2048 # 2GB or 2048MB
$WarningLevel = 1800 # 1800MB

ForEach ($O365Group in $O365Groups){ 
If($O365Group.SharePointSiteUrl -ne $null) {
$siteUrl = $O365Group.SharePointSiteUrl
Set-SPOSite -Identity $siteUrl -StorageQuota $StorageQuota -StorageQuotaWarningLevel $WarningLevel 
}
}
Once you set the storage quota, you can use the Powershell cmdlet Get-SPOSite to get current storage quota and warning level. The following script list storage quota of all the office 365 groups site.

$O365Groups = Get-UnifiedGroup -ResultSize Unlimited
$CustomResult=@() 
ForEach ($O365Group in $O365Groups){ 
If($O365Group.SharePointSiteUrl -ne $null) 
{ 
   $O365GroupSite=Get-SPOSite -Identity $O365Group.SharePointSiteUrl 
   $CustomResult += [PSCustomObject] @{ 
     GroupName =  $O365Group.DisplayName
     SiteUrl = $O365GroupSite.Url 
     StorageQuota_inGB = $O365GroupSite.StorageQuota/1024
     WarningSize_inGB =  $O365GroupSite.StorageQuotaWarningLevel/1024
     CurrentStorage_inMB = $O365GroupSite.StorageUsageCurrent
  }
}} 
 
$CustomResult | FT

Thursday, 16 June 2016

Get the storage used by Office 365 groups using Powershell

Office 365 Groups are nothing but a hidden site collection with mailbox that are not visible in Site Collections view in Office 365 tenant Admin portal. You can only access these site collections by using PowerShell or through URL (https://<tanentname>.sharepoint.com/sites/<group-name>/Shared documents”). Often, Office 365 administrators need to find the storage used by Office 365 groups since this storage gets the storage quota of the SharePoint Site Collections. In this post, I am going to write powershel script to find storage used by office 365 groups.

We can use the SharePoint Online Powershell cmdlet Get-SPOSite to get current site storage size and storage quota. Before proceed, run the following command to connect Sharepoint Online powershell module.
Connect-SPOService -Url https://<tanentname>-admin.sharepoint.com -Credential admin@o365domain.com
Now run the below script after replacing the <tanentname> and <group-name> with your own tenant name and group name.
$O365GroupSiteUrl ="https://<tanentname>.sharepoint.com/sites/<group-name>"
$O365GroupSite = Get-SPOSite -Identity $O365GroupSiteUrl
$StorageSize =$O365GroupSite.StorageUsageCurrent 
                
Write-Host "Storage  used (MB): " $StorageSize " MB" -ForegroundColor Yellow
Write-Host "Storage  used (GB): " ($StorageSize/1024) " GB" -ForegroundColor Yellow

Get the current Storage Size for all Office 365 Groups:

To get the storage used by all Office 365 Groups, first, we need to get sharepoint site url for all the office 365 groups by using Exchange Online cmdlet Get-UnifiedGroup. The following powershell script gets current storage size and storage quota of all the office 365 groups. You need to replace your own Office 365 tenant name and admin credentials.
$userName ="admin@<tanentname>.onmicrosoft.com" 
$o365Cred = Get-Credential -UserName $userName -Message "Enter Office 365 Admin Credentials"

$o365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $o365Cred -Authentication Basic -AllowRedirection
Import-PSSession $o365Session

$spoAdminUrl ="https://<tanentname>-admin.sharepoint.com/" 
Connect-SPOService -Url $spoAdminUrl -Credential $o365Cred 

$O365Groups = Get-UnifiedGroup -ResultSize Unlimited

$CustomResult=@() 

ForEach ($O365Group in $O365Groups){ 
If($O365Group.SharePointSiteUrl -ne $null) 
{ 
   $O365GroupSite=Get-SPOSite -Identity $O365Group.SharePointSiteUrl 
   $CustomResult += [PSCustomObject] @{ 
     GroupName =  $O365Group.DisplayName
     SiteUrl = $O365GroupSite.Url 
     StorageUsed_inMB = $O365GroupSite.StorageUsageCurrent
     StorageQuota_inGB = $O365GroupSite.StorageQuota/1024
     WarningSize_inGB =  $O365GroupSite.StorageQuotaWarningLevel/1024
  }
}} 
 
$CustomResult | FT
You can also export the output into csv file:
$CustomResult | Export-CSV "C:\\O365-Group-Storage-Info.csv" -NoTypeInformation -Encoding UTF8

Thursday, 9 June 2016

Convert Int64 TimeStamp to DateTime in Powershell

Some Applications (Ex: Active Directory ) stores DateTime value as TimeStamp value in a way to support different time zone. The Int64 TimeStamp is nothing but Windows file time. The Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC).

In Powershell, we can use the .Net function FromFileTime and convert the output to DateTime format.
$timestamp = "131099683087123361"
[DateTime]::FromFileTimeutc($timestamp)
You can also convert the standard datetime to timestamp value by using the function ToFileTimeUtc.
$date = Get-Date
$date.ToFileTimeUtc()
In Active Directory environment, the attributes LastLogonTimeStamp and PwdLastSet are stored as Int64 TimeStamp. When you query these properties by using Get-ADUser cmdlet, you need to explicitly convert LastLogonTimeStamp value into datetime value.
Get-ADUser -Identity 'Smith' -Properties LastLogonTimeStamp | 
Select-Object -Property "Name", @{n="LastLogon";e={[datetime]::FromFileTime($_."LastLogonTimeStamp")}}
The following powershell command convert AD user's PwdLastSet value to datetime value.
Get-ADUser -Identity 'Smith' -Properties PwdLastSet | 
Select-Object -Property "Name", @{n="PwdLastSet";e={[datetime]::FromFileTime($_."PwdLastSet")}}

Wednesday, 8 June 2016

List Office 365 Group Members using Powershell

The post helps you to list office 365 group (not distribution group) members by using powershell script. We can list all the office 365 groups by using the powershell cmdlet Get-UnifiedGroup and its group members by Get-UnifiedGroupLinks cmdlet.

Note: Before proceed, Connect Exchange Online Remote PowerShell.

The following command lists all the office 365 groups.
Get-UnifiedGroup | Select DisplayName,GroupType,PrimarySmtpAddress

List Office 365 Group Members

We can use the powershell cmdlet Get-UnifiedGroupLinks to view the members of an existing group. The key parameters for this cmdlet are:

Identity – the alias of the group
LinkType – Members, Owners, or Subscribers. Required.

Use the below powershell command to select members of a single office 365 group.
Get-UnifiedGroupLinks -Identity '<group-name>' -LinkType Members
If you want to list members of all the office 365 groups, first, we need to get the results of Get-UnifiedGroup, then we can pipe the output to ForEach-Object and get members for all the office 365 groups.
$Groups = Get-UnifiedGroup -ResultSize Unlimited
$Groups | ForEach-Object {
$group = $_
Get-UnifiedGroupLinks -Identity $group.Name -LinkType Members | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group.DisplayName
       Member = $_.Name
       EmailAddress = $_.PrimarySMTPAddress
       RecipientType= $_.RecipientType
}}}

Export All Office 365 Group Members to CSV

We can export powershell output into CSV file using Export-CSV cmdlet. The following command exports all the office 365 group members to CSV file.
$Groups = Get-UnifiedGroup -ResultSize Unlimited
$Groups | ForEach-Object {
$group = $_
Get-UnifiedGroupLinks -Identity $group.Name -LinkType Members | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group.DisplayName
       Member = $_.Name
       EmailAddress = $_.PrimarySMTPAddress
       RecipientType= $_.RecipientType
}}} |

Export-CSV "C:\\Office365GroupMembers.csv" -NoTypeInformation -Encoding UTF8

Tuesday, 7 June 2016

List all Parameters for a Cmdlet in Powershell

When you start work with a new Powershell cmdlet, you might want to get a list of all the available parameters in the powershell cmdlet. We can use the command GET-Command to display all the parameters.
 (GET-Command GET-Process).parameters
We can also use the command Get-Help to display all the available parameters with details.
Get-Help GET-Process -Parameter *
If you want to view only required or mandatory parameters in a cmdlet, we can filter the results using Where-Object with Required property.:
Get-Help GET-Process -Parameter * | Where-Object {$_.Required -eq $true}