I've been trying to run different PowerShell scripts to get all or subsites that are into a site collection, however when I run this ask for the Windows PowerShell credential that haven't able to figure out to get the correct one or kind of username there
This one is an example of any of scripts I've been trying to run to figure how to fetch these subsites:
#Provide your SharePoint Online Admin center URL
$AdminSiteURL = "**"
#Get SharePoint Admin User Credentials
$Cred = Get-Credential
#Connect to SharePoint Admin Site
Connect-PnPOnline -Url $AdminSiteURL -Credentials $Cred
#Get all site collections
$Sites = Get-Collaboration-SiteTesting
#The below command gets only modern Team & Communication sites
#$Sites = Get-Collaboration-SiteTesting | Where -Property Template -In ("", "SITEPAGEPUBLISHING#0")
$AllSites = @()
$i = 0;
$TotoalSites = $Sites.Count
#Enumerate site collections and get sub sites recursively
ForEach($Site in $Sites)
{
$i++;
Write-Progress -activity "Processing $($Site.Url)" -status "$i out of $TotalSites completed"
$SubWebs=$null;
Try
{
#Connect to site collection
$SiteConnection = Connect-PnPOnline -Url $Site.Url -Credentials $Cred
#Get the sub sites of the site collection
$SubWebs = Get-PnPSubWeb -Recurse -Connection $SiteConnection
Disconnect-PnPOnline -Connection $SiteConnection
}
catch{
Write-Host "Error occured $($Site.Url) : $_.Exception.Message" -Foreground Red;
}
#Add site collection in AllSites list
$AllSites += New-Object PSObject -property $([ordered]@{
SiteName = $Site.Title
SiteURL = $Site.Url
IsSubSite = $false
HasSubSites = if ($SubWebs -and $SubWebs.Count -gt 0) { $true } Else {$false}
SiteCollectionName = $Site.Title
SiteCollectionURL = $Site.Url
})
if ($SubWebs -and $SubWebs.Count -gt 0) {
#Enumerate sub sites and add in AllSites list
ForEach($SubSite in $SubWebs)
{
$AllSites += New-Object PSObject -property $([ordered]@{
SiteName = $SubSite.Title
SiteURL = $SubSite.Url
IsSubSite = $true
HasSubSites = $false
SiteCollectionName = $Site.Title
SiteCollectionURL = $Site.Url
})
}
}
}