Export all sites and subsites to a text file with PowerShell

For a project I needed to create a textfile that contains all the sites and subsites in a given site collection. I used the following PowerShell script to do this:

PS C:\Users\Mossadmin> get-spsite ‘siteurl’ | Get-SPWeb -Limit ALL | sort url,title | select title, url | out-string -width 512 >filename.txt