$LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
$LogFile = ".\GetSubsitePatch-$LogTime.rtf"
# Add SharePoint PowerShell Snapin
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
Add-PSSnapin Microsoft.SharePoint.Powershell
}
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase
#Deleting any .rtf files in the scriptbase location
$FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
if($FindRTFFile)
{
foreach($file in $FindRTFFile)
{
remove-item $file
}
}
start-transcript $logfile
$Output = $scriptBase + "\" + "SiteDetails.csv";
"WebApplication" + "," + "SiteCollection" + "," + "Webs" | Out-File -Encoding Default -FilePath $Output;
$webapp = read-host "Enter the web app URL under which you want to list out the subsites"
$testWebApp = get-spwebapplication $webapp -ea silentlycontinue
if($testWebApp -ne $null)
{
$empty = ""
$webapp + "," + $empty + "," + $empty | Out-File -Encoding Default -Append -FilePath $Output;
$sites = get-spsite -limit all -webapplication $webapp
foreach($site in $sites)
{
$empty + "," + $site.url + "," + $empty | Out-File -Encoding Default -Append -FilePath $Output;
foreach($web in $site.allwebs)
{
Write-host $web.url -fore cyan
$empty + "," + $empty + "," + $web.url | Out-File -Encoding Default -Append -FilePath $Output;
}
}
}
else
{
write-host "Invalid Web app.... please check the URL" -fore cyan
}
stop-transcriptIn this code snippet you will learn to get the lists of sites under a web application.
