title: Sharepoint Online - List sharepoint sites in a sitecollection using powershell
body: <p>i want so list all sites in a sitecollection in sharepoint online using powershell. I am using the following script at the moment:</p>
  
  <pre><code>Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
  Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
  
  $siteUrl = “https://mytenant.sharepoint.com/sites/mysitecollection” 
  $username = "myusername" 
  $password = Read-Host -Prompt "Enter password" -AsSecureString  
  $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
  $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
  $ctx.Credentials = $credentials 
  
  $rootWeb = $ctx.Web  
  $sites  = $rootWeb.Webs 
  
  $ctx.Load($rootWeb) 
  $ctx.Load($sites) 
  $ctx.ExecuteQuery() 
  
  foreach($site in $sites) 
  { 
      $ctx.Load($site) 
      $ctx.ExecuteQuery() 
  
      Write-Host $site.Title "-" $site.Url  
  } 
  </code></pre>
  
  <p>The errors look like:</p>
  
  <pre><code>Exception calling " executeQuery " with 0 Argument ( s) : " The remote server returned an error : ( 401 ) Unauthorized . "
  Line : 16 Char: 1
  
  The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
  In Zeile:18 Zeichen:9
  + foreach($site in $sites)
  </code></pre>
  
  <p>Can somebody help me please?</p>