Microsoft 365 : Microsoft Graph PowerShell tutorial – Part 6 – exploring Get-MgSite

Microsoft Graph PowerShell - Exploring Get-MgSite
Microsoft Graph PowerShell - Exploring Get-MgSite

Hi All,

Greetings for the day!

Continuing on my favorite topic – Microsoft Graph Powershell.

Today we will discussing one of the important CMDLET – Get-MgSite

Get-MGSite

  • This CMDLET is required to get particular site details
  • This CMDLET is part of module – Microsoft.Graph.Sites
  • Permissions (delegated permissions) required to execute – Get-MgSite
    • Sites.Read.ALL
  • SYNTAX
Get-MgSite
   [-ExpandProperty <String[]>]
   [-Property <String[]>]
   [-Filter <String>]
   [-Search <String>]
   [-Skip <Int32>]
   [-Sort <String[]>]
   [-Top <Int32>]
   [-PageSize <Int32>]
   [-All]
   [-CountVariable <String>]
   [<CommonParameters>]
Get-MgSite
   -SiteId <String>
   [-ExpandProperty <String[]>]
   [-Property <String[]>]
   [<CommonParameters>]

Executing the CMDLET – Connecting to Microsoft Graph using Connect-MgGraph

  • As discussed in one of the article – Microsoft 365 : PowerShell – Microsoft Graph PowerShell tutorial – Part 1https://knowledge-junction.in/2023/08/30/microsoft-365-powershell-microsoft-graph-powershell-tutorial-part-1/, we need to invoke – Connect-MgGraph before executing any Microsoft Graph PowerShell CMDLET.
  • This CMDLET (Connect-MgGraph) get access token using Microsoft Authentication Library
  • Here we will use delegated access permission and interactive authentication as in below snap. We are executing the CMDLET from Windows PowerShell ISE

Connect-MgGraph -Scopes "Sites.Read.All"

  • Here scope parameter specifies – An array of delegated permissions to consent to.
  • When we execute this CMDLET we get prompt for credentials
  • As we connected the Microsoft Graph successfully, we are ready to execute OUR CMDLETs
Microsoft Graph PowerShell - Exploring Get-MgSite - Connecting to Microsoft Graph with permissions - "Sites.Read.All" - Connect-MgGraph
fig : Microsoft Graph PowerShell – Exploring Get-MgSite – Connecting to Microsoft Graph with permissions – “Sites.Read.All” – Connect-MgGraph

ExamplesGet-MgSite

  • Example 1 – Search all sites with keyword – Demo

Get-MgSite -search demo

Output:

Id                                                                                                          DisplayName                      Name                        
--                                                                                                          -----------                      ----                        
knowledgejunction1.sharepoint.com,78aaa1ac-73f6-494c-8596-53cb5251fec1,f7b914d6-a4dc-4819-9eaa-bd3629621aa7 Demo                             Demo                        
knowledgejunction1.sharepoint.com,1a22eb80-09e2-44eb-a8a3-124fdb294b48,851fecab-3fc8-4b9d-add8-91866cabb2f4 Demo Remove User Permissions     demoremoveuserpermissions   
knowledgejunction1.sharepoint.com,5e4679c7-8646-4380-bf33-9ecac5769e8d,80dc1529-dc01-4c97-a421-bc61127d72aa Blogs Demo                       BlogsDemo                   
knowledgejunction1.sharepoint.com,c7117a91-c034-43e8-b335-5b5418a39d44,f7b914d6-a4dc-4819-9eaa-bd3629621aa7 SharePoint App Bar Demo          SharePointAppBarDemo        
knowledgejunction1.sharepoint.com,59e1907a-bd11-4f00-af1b-b2ab19f24dac,5de3c42f-bee2-4a67-a1fa-b034bc9da1c0 App Installation On Subsite Demo appinstallation             
knowledgejunction1.sharepoint.com,faaf70ba-780e-4f8e-a3ec-c7f632ab4691,f5d8fc1b-fca4-4325-8740-bc38100be195 SPS_Demo_SiteLifeCycle_24.03.20  sps_demo_sitelifecycle_24...
knowledgejunction1.sharepoint.com,237c32a1-0683-4fbf-ae85-d0231608241e,5aa56e36-22be-4f34-8093-650b39c2be27 spswapdemo                       knowledgejunction1.sharep...
knowledgejunction1.sharepoint.com,78aaa1ac-73f6-494c-8596-53cb5251fec1,316d488f-d1a3-4e97-a4fa-d896f1e68900 DemoPermissions                  PermissionsDemo             
knowledgejunction1.sharepoint.com,3d53cd5b-5064-4665-ac14-f512b6636b60,0dd4435c-8a5e-449e-b3a4-b80a84bd60eb SPGovernance                     SPGovernance                
knowledgejunction1.sharepoint.com,78aaa1ac-73f6-494c-8596-53cb5251fec1,748faf55-e7cc-4a9d-a258-5a865c3d2f4f DemoSpaceInURL                   Space In URL 

Microsoft Graph PowerShell - Exploring Get-MgSite - Listing all sites in Tenant having keyword in DisplayName, Name and Description of sites
fig : Microsoft Graph PowerShell – Exploring Get-MgSite – Listing all sites in Tenant having keyword in DisplayName, Name and Description of sites

  • Example 2 Get specific site with site ID – Demo
    • Here if we dont know specific site ID – we could form it – <domain>.sharepoint.com/sites/<sitename> – as in below snap

#Example 2 - Get specific site details with "siteid" parameter - kindly please note the value for #siteid parameter
Get-MgSite -siteid "knowledgejunction1.sharepoint.com:/sites/demo" |fl

Output:

PS C:\> Get-MgSite -siteid "knowledgejunction1.sharepoint.com:/sites/demo"

Id                                                                                                          DisplayName Name WebUrl                                      
--                                                                                                          ----------- ---- ------                                      
knowledgejunction1.sharepoint.com,78aaa1ac-73f6-494c-8596-53cb5251fec1,f7b914d6-a4dc-4819-9eaa-bd3629621aa7 Demo        Demo https://knowledgejunction1.sharepoint.com...



PS C:\> Get-MgSite -siteid "knowledgejunction1.sharepoint.com:/sites/demo" |fl


Analytics            : Microsoft.Graph.PowerShell.Models.MicrosoftGraphItemAnalytics1
Columns              : 
ContentTypes         : 
CreatedBy            : Microsoft.Graph.PowerShell.Models.MicrosoftGraphIdentitySet
CreatedByUser        : Microsoft.Graph.PowerShell.Models.MicrosoftGraphUser
CreatedDateTime      : 8/3/2022 12:06:18 PM
Description          : 
DisplayName          : Demo
Drive                : Microsoft.Graph.PowerShell.Models.MicrosoftGraphDrive
Drives               : 
ETag                 : 
Error                : Microsoft.Graph.PowerShell.Models.MicrosoftGraphPublicError
ExternalColumns      : 
Id                   : knowledgejunction1.sharepoint.com,78aaa1ac-73f6-494c-8596-53cb5251fec1,f7b914d6-a4dc-4819-9eaa-bd3629621aa7
Items                : 
LastModifiedBy       : Microsoft.Graph.PowerShell.Models.MicrosoftGraphIdentitySet
LastModifiedByUser   : Microsoft.Graph.PowerShell.Models.MicrosoftGraphUser
LastModifiedDateTime : 1/29/2024 6:02:01 AM
Lists                : 
Name                 : Demo
Onenote              : Microsoft.Graph.PowerShell.Models.MicrosoftGraphOnenote1
Operations           : 
ParentReference      : Microsoft.Graph.PowerShell.Models.MicrosoftGraphItemReference
Permissions          : 
Root                 : Microsoft.Graph.PowerShell.Models.MicrosoftGraphRoot
SharepointIds        : Microsoft.Graph.PowerShell.Models.MicrosoftGraphSharepointIds
SiteCollection       : Microsoft.Graph.PowerShell.Models.MicrosoftGraphSiteCollection
Sites                : 
TermStore            : Microsoft.Graph.PowerShell.Models.MicrosoftGraphTermStore1
TermStores           : 
WebUrl               : https://knowledgejunction1.sharepoint.com/sites/Demo
AdditionalProperties : {[@odata.context, https://graph.microsoft.com/v1.0/$metadata#sites/$entity]}

Microsoft Graph PowerShell - Exploring Get-MgSite - Get specific site with site ID
fig : Microsoft Graph PowerShell – Exploring Get-MgSite – Get specific site with site ID

Example 3 – Get specific site with site ID with specific properties

  • By default, Get-MgSite returns – Id, Name, DisplayName and URL properties
  • To get additional required properties we need to specify those properties explicitly with –Property attribute as in below snap / code

#Example 3 - Get specific site details with "siteid" parameter and with selected properties- kindly please note the value for siteid parameter
$site = Get-MgSite -siteid "knowledgejunction1.sharepoint.com:/sites/demo" -Property "CreatedDateTime, LastModifiedDateTime"
"Site created on - " + $site.CreatedDateTime
"Site last modified on - " + $site.LastModifiedDateTime

Output:
Site created on - 08/03/2022 12:06:18
Site last modified on - 01/29/2024 16:40:43

Microsoft Graph PowerShell - Exploring Get-MgSite - Get specific site with site ID with specific properties
fig : Microsoft Graph PowerShell – Exploring Get-MgSite – Get specific site with site ID with specific properties

Microsoft Graph APIs are way to go now for Microsoft 365. We have very good series of articles on Microsoft Graph. Kindly please have a look – https://knowledge-junction.in/?s=microsoft+graph OR https://knowledge-junction.in/category/technology-articles/m365/microsoft-graph/

REFERENCES

Thanks for reading ! Stay tuned for more articles on PowerShell and Microsoft Graph !

HAPPY LEARNING AHEAD 🙂 LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading