Get all Sites and Subsites of a webapplication using powershell

Sometimes we need to list all the sites and subsites and sub-subsites of one of the web application. We can do this by either coding or by powershell scripting. Coding is a tedious task so can prefer powershell as long as you have access to the server.
 
  1. ## SharePoint DLL  
  2. [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")  
  3.   
  4. $webApplicationURL = Read-Host "Enter Web application"  
  5.   
  6. $webApp = Get-SPWebApplication $webApplicationURL  
  7.   
  8. if($webApp -ne $null)  
  9. {  
  10. #Write-Host "Web Application : " + $webApp.Name  
  11. foreach($siteColl in $webApp.Sites)  
  12. {  
  13. if($siteColl -ne $null)  
  14. {  
  15. Write-Host -foregroundcolor red "Site Collection: "$siteColl.Url  
  16. Get-SPSite $siteColl | Get-SPWeb -Limit All | Select Title, Url  
  17. }  
  18. else  
  19. {  
  20. Echo $siteColl "does not exist"  
  21. }  
  22. }  
  23. }  
  24. else  
  25. {  
  26. Write-Host  $webApplicationURL "does not exist, check the WebApplication name"  
  27. }