I would suggest that you download and install the PnP PowerShell
. It is wrapper on the OOTB SharePoint powershell but with much easier commandlets and ease of use.
After you download and install the PnP PowerShell
, you can run the below commands:
Connect-PnPOnline -Url "<your-site-url>"
Get-PnPSiteSearchQueryResults -Query "(contentclass:'STS_Site') (contentclass:'STS_Web')" -All
After you run the command, Connect-PnPOnline -Url "<your-site-url>"
, it will open a window asking for credentials. If you provide your credentials, it will work in that context. If you provide admin credentials, it will work in admin context.
Get-PnPSiteSearchQueryResults
submits a search query which is the fastest and most efficient way to get list of all the sites and subsites.
Do note, this will give the results of all the sites and subsites to which only you have access. If you run in the context of admin, then, if and only if the admin is added to that particular site will he have access to it.
References -
Dowload this file - SharePointPnPPowerShellOnline.msi
for SharePoint online.
Download links (get the latest Nov 2017 or higher) - PnP-PowerShell
Documentation - Get-PnPSiteSearchQueryResults