Tuesday, 19 June 2018

How to Install and Connect SharePoint Online PowerShell Module

SharePoint Online team focusing Microsoft Graph Api to provide common api experience for Office 365 users, but some Admin related activities still we need to use powershell module. Follow the below steps to install and connect SharePoint Management Shell.
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Note: If powershell console is already opened, close and open the powershell again after installing the above setup.
  • Now run the following command after replacing your tenant name to connect spo service. Actually you have to set your spo admin site url, you can refer this post for more info: how to get SharePoint Online Admin Site Url
Connect-SPOService -url https://<your tenant name>-admin.sharepoint.com
  • Once you have connected the SPO service, then you can run SPO management commands. The below command lists all spo sites.
Get-SPOSite | Select Title, Url

Friday, 15 June 2018

How to increase Windows service startup timeout period

Problem:

Some software services may failed to start in slower machine and you will get following error events in Application event log.

Event ID: 7000

The ServiceName service failed to start due to the following error:
The service did not respond to the start or control request in a timely fashion.

Event ID: 7009

A timeout was reached (30000 milliseconds) while waiting for the ServiceName service to connect.

Solution:

To fix this service startup problem, we need to increase default startup timeout period 30000 milliseconds (30 seconds). Follow the below steps to increase all windows services startup timeout.
  • Open Run window by clicking Start button -> click Run.
  • In Run window, type regedit, and then click OK to open Registry Editor.
  • Locate and then click the following registry subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
  • In the right pane, locate the ServicesPipeTimeout entry.
Note: If the ServicesPipeTimeout entry does not exist, first you should create it. Follow below steps:
  1. On the Edit menu, point to New, and then click DWORD Value.
  2. Type ServicesPipeTimeout, and then press ENTER.
  • One you locate (or created) ServicesPipeTimeout, right-click ServicesPipeTimeout, and then click Modify. Click Decimal, type 120000 (2 mins), and then click OK.
This value (120000) represents the time in milliseconds to wait for windows service to start before timeout.
  • Restart the computer once you done the changes.
How to increase Windows service startup timeout period

Thursday, 14 June 2018

Find and Export all SharePoint Online Sites using Powershell

In this post, I am going to share powershell script to get a list of sites in Office 365. We can easily extract all sites using the SharePoint Online Management shell cmdlet Get-SPOSite.

Before proceed Install the SharePoint Online Management Shell:
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Note: If powershell console already opened, close and open the powershell again after installing the above setup.

We need admin site url to connect SharePoint Online service, you can refer this post to Get SharePoint Online Admin Site Url. Once you got the admin site url run the below script after replacing your AdminUrl to list all sites.
#Replace your admin site url
$AdminUrl = "https://contoso-admin.sharepoint.com/"
#Retrieve all site collections
Connect-SPOService -Url $AdminUrl
Get-SPOSite | Select Title, Url, Owner
If you want to retrieve all the available properties, run the below command, it will list all the attributes of first site.
Get-SPOSite | Select -First 1 | FL
You can also filter the sites based on any available property, the below command lists only sites with sharing capability enabled as ExternalUserSharingOnly.
Get-SPOSite | Where-Object {$_.SharingCapability -eq "ExternalUserSharingOnly"}
Run the below command to list sites with share capability disabled.
Get-SPOSite | Where-Object {$_.SharingCapability -eq "Disabled"}

Export all SharePoint Online Sites to CSV:

Run the below command to export all the required details of spo sites to csv file.
Get-SPOSite | Select Title, Url, Owner, SharingCapability, LastContentModifiedDate |
Export-CSV "C:\\SharePoint-Online-Sites.csv" -NoTypeInformation -Encoding UTF8

Tuesday, 12 June 2018

Powershell : The term 'Connect-SPOService' is not recognized as the name of a cmdlet

Problem :

I have got the error message "The term 'Connect-SPOService' is not recognized as the name of a cmdlet" when I run the powershell cmdlet Connect-SPOService to connect SharePoint Online site.
Connect-SPOService : The term 'Connect-SPOService' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Connect-SPOService -url https://conotoso-admin.sharepoint.com
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Connect-SPOService:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Solution :

The command Connect-SPOService belongs to SharePoint Online Management Shell, so before using this command we need to install the SPO Management Shell and import the module. You only need install once for each computer from which you are running SPO Powershell commands.

Install SharePoint Online Management Shell:

You can download and install the setup from this path : Download SharePoint Online Management Shell

Connect SharePoint Online Module:

Once you have installed the above setup, run the following command to import the SharePoint online powershell module.
Import-Module Microsoft.Online.SharePoint.Powershell
Then run the following commands to connect spo service and list all sites.
Connect-SPOService -url https://conotoso-admin.sharepoint.com 
Get-SPOSite | Select Title, Url

Friday, 8 June 2018

Office 365 : How to get SharePoint Online Admin Site Url

You have to give your SharePoint admin site url as root scope when you work with SharePoint online PowerShell module and SharePoint Client Object Model (CSOM). The possible admin site url would be like below sample url.
https://tenantname-admin.sharepoint.com/
Consider your office 365 user id (userPrincipalName) is : "admin@contoso.onmicrosoft.com" and most probably your tenant name is "contoso" then your sharepoint admin site url should be below one.
https://contoso-admin.sharepoint.com/
You can test the url is correct or not by using this url as root scope with SharePoint Online Powershell module. The below connect your sharepoint admin site and list all sites.
$cred = Get-Credentials
Connect-SPOService -url "https://contoso-admin.sharepoint.com" -Credential $credential
Get-SPOSite | Select Title, Url
You can also get the correct url by connecting Office 365 Admin center site:
Steps to get sharepoint admin site url from Office 365 admin center
  • Now, you can view your admin site and get the correct admin site url as shown in below image.
how to get sharepoint admin site url

Thursday, 7 June 2018

Powershel : Convert JSON String to PS Object and Vice-versa

Many software services moving towards cloud environment and providing APIs to communicate with service. To standardize this communication many software APIs use JSON format to receive and return data. In this post, I am going to share powershell script to convert json to object and convert object to json string.

Convert JSON content to PS Object :

You can easily convert json text into object using the powershell cmdlet ConvertFrom-Json. Consider the below sample JSON that might be a HTTP/HTTPS response content returned from some API.
$usersJson = '{"users":[
    { "username":"kevin", "email":"kevin@test.com" },
    { "username":"morgan", "email":"morgan@test.com" },
    { "username":"smith", "email":"smith@test.com" }
]}'
Run the following command to convert the above json string to ps object.
$usersObject = ConvertFrom-Json –InputObject $usersJson
Now you can easily list the ps object properties
PS C:\> $usersObject.users

username email
-------- -----
kevin    kevin@test.com
morgan   morgan@test.com
smith    smith@test.com

Convert PS Object to JSON String :

You can convert the above ps object into json text using the powershell cmdlet ConvertTo-Json.
$newUsersJson = $usersObject | ConvertTo-Json

parse json string as ps object and convert ps object to json string

Wednesday, 6 June 2018

Powershell : The term 'Get-AzureADUser' is not recognized as the name of a cmdlet

Problem :

You might have received the error message "The term Get-AzureADUser is not recognized as the name of a cmdlet" when you run the Azure AD powershell cmdlet Get-AzureADUser to get Office 365 users.
The term 'Get-AzureADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:16
+ Get-AzureADUser <<<<
    + CategoryInfo          : ObjectNotFound: (Get-AzureADUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Solution :

The command Get-AzureADUser belongs to Azure Active Directory Powershell for Graph module, so before using this command we need to install and connect AzureAD powershell v2 module.

Install AzureAD module:

Open Powershell console with Run as administrator privilege and run the following command:
Install-Module AzureAD -Force

Connect/Load AzureAD module:

Once you have installed the Azure AD module, run the following command to load the module.
Connect-AzureAD
# Once you connected the AzureAD module, now run the Get-AzureADUser command.
Get-AzureADUser

Powershell : The term 'Get-MsolUser' is not recognized as the name of a cmdlet

Problem :

You may receive the error "The term Get-MsolUser is not recognized as the name of a cmdlet" when you run the Azure AD powershell command Get-MsolUser to fetch Office 365 users.
The term 'Get-MsolUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:13
+ Get-MsolUser <<<<
    + CategoryInfo          : ObjectNotFound: (Get-MsolUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Solution :

The cmdlet Get-MsolUser belongs to Azure AD Powershell (V1) module (MSOnline), so before using this command we need to install and connect MSOnline module.

Install MSOnline module:

Earlier you need to download an executable (exe binary) file to install this module, but now Microsoft expired that file and you can install only through powershell. Open Powershell console with Run as administrator privilege and run the following command:
Install-Module MSOnline -Force

Connect/Import MSOnline module:

Once you have installed the MSOnline module, run the following command to connect MSOnline module.
Connect-MsolService
# Once you connect MsolService, now run the Get-MsolUser command.
Get-MsolUser

Tuesday, 5 June 2018

Find and List MFA Enabled Status of Office 365 Users using Powershell

Multi-Factor Authentication (MFA) is a method of Azure AD authentication that requires more than one verification method and adds a critical second layer of security to user sign-ins and transactions. You can easily enable MFA solution for Azure AD users using Azure MFA portal. In this post, I am going to share powershell script to list office 365 users MFA status.

List Office 365 Users MFA Status:

Before proceed run the following command to connect Azure AD powershell module.
Connect-MsolService
The following command lists MFA status of all the Azure AD users.
$Result=@() 
$users = Get-MsolUser -All
$users | ForEach-Object {
$user = $_
if ($user.StrongAuthenticationRequirements.State -ne $null){
$mfaStatus = $user.StrongAuthenticationRequirements.State
}else{
$mfaStatus = "Disabled" }
  
$Result += New-Object PSObject -property @{ 
UserName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
MFAStatus = $mfaStatus
}
}
$Result | Select UserName,UserPrincipalName,MFAStatus

Export 365 users MFA status to CSV file :

$Result | Select UserName,UserPrincipalName,MFAStatus |
Export-CSV "C:\\O365-Users-MFA-Status.csv" -NoTypeInformation -Encoding UTF8

List only MFA enabled users :

$Result | Where-Object {$_.MFAStatus -ne "Disabled"}

Monday, 4 June 2018

Hide Office 365 Groups from Outlook and OWA using Powershell

As you know Office 365 Group is the base service for other O365 services like Planner, Teams, Yammer, etc... So in some situations you may need to hide some groups from Exchange clients like Outlook and OWA. If you use Microsoft Teams, every team will use its own office 365 group as base service, as you know Teams is the best communication tool to have complete conversation within a team, so there is no need to use teams group in exchange client for conversation, in this case we can hide teams based office 365 groups in Exchange clients.

Hiding Office 365 groups from Exchange clients is easily achievable by using the Exchange Online Powershell cmdlet Set-UnifiedGroup with the parameter -HiddenFromExchangeClientsEnabled. Run the following command to hide a single office 365 group from OWA and Outlook client.
Set-UnifiedGroup "TeamsO365Group" -HiddenFromExchangeClientsEnabled:$true
You can disable the setting by just passing he value $false to the parameter HiddenFromExchangeClientsEnabled
Set-UnifiedGroup "TeamsO365Group" -HiddenFromExchangeClientsEnabled:$false

Lists Office 365 Groups Exchange Client Status :

You can easily check the exchange client status of all office 365 groups by running below command.
 Get-UnifiedGroup -ResultSize Unlimited | Select Name, HiddenFromExchangeClientsEnabled 
The below command lists only the groups that are hidden from exchange client.
 Get-UnifiedGroup -ResultSize Unlimited | Where-Object {$_.HiddenFromExchangeClientsEnabled -eq $true}

Export Office 365 Groups Exchange client status to CSV:

 Get-UnifiedGroup -ResultSize Unlimited | Select Name, HiddenFromExchangeClientsEnabled |
Export-CSV "C:\\O365-Groups-Exchange-Client-Status.csv" -NoTypeInformation -Encoding UTF8

Hide Bulk Office 365 Groups from Outlook and OWA :

We may required to hide multiple office 365 groups in single execution, in this case we can have group names in csv. We need to import csv file, and then pass every group to Set-UnifiedGroup cmdlet. Consider the csv file TeamGroups.csv that has unified groups with the column header GroupName.
Import-Csv 'C:\TeamGroups.csv' | ForEach-Object {
$o365group = $_."GroupName"
Set-UnifiedGroup $o365group -HiddenFromExchangeClientsEnabled:$true
} 

Note: All O365 Groups in which HiddenFromExchangeClientsEnabled is set to True will not be visible on the left navigation menu in Outlook and OWA, but you can still access them on demand (ex: use Browse Groups in Outlook)

Friday, 1 June 2018

Export Office 365 Users Mailbox Size to CSV using Powershell

Occasionally we need to get how much space is used by every office 365 user in their mailbox. In this post, I am going to share powershell script to export mailbox size, mail items count and last logon date of all users in your O365 tenant. We can use the Exchange Online powershell cmdlet Get-MailboxStatistics to extract mailbox statistics data.

Before proceed, first we need to connect Exchange Online powershel module by running below command:
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Run the following command to get mailbox size for a single Office 365 user:
Get-MailboxStatistics -Identity "alexw@contoso.com" | Select TotalItemSize

Export Office 365 users mailbox size, mails count and last logon date:

Run the following powershell script to export all o365 users mailbox sizes to CSV file.
$Result=@() 
$mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
$totalmbx = $mailboxes.Count
$i = 1 
$mailboxes | ForEach-Object {
$i++
$mbx = $_
$mbs = Get-MailboxStatistics -Identity $mbx.UserPrincipalName

if ($mbs.LastLogonTime -eq $null){
$lt = "Never Logged In"
}else{
$lt = $mbs.LastLogonTime }
 
Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
 
$Result += New-Object PSObject -property @{ 
UserPrincipalName = $mbx.UserPrincipalName
TotalSize = $mbs.TotalItemSize
TotalMessages = $mbs.ItemCount
LastLogonTime = $lt }
}
$Result | Select UserPrincipalName, TotalSize, TotalMessages, LastLogonTime |
Export-CSV "C:\\O365-Mailbox-Statistics.csv" -NoTypeInformation -Encoding UTF8

CSV Output of Mailbox Statistics Report:


export office 365 users mailbox sizes to csv using powershell