How to execute commands in SharePoint 2010 PowerShell?

Steps

Write PowerShell code in notepad -> save notepad file with .ps1 extension, i.e. ‘getSiteName.ps1 -> Save it in c:\ -> execute this file using following command with file name:
c:\ .\getSiteName.ps1

Your command will get executed and result will be displayed.

Example

Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

$spSite = Get-SPSite -Identity "http:/abcd/"
foreach ($spWeb in $spSite.AllWebs)
{
 
                $spWeb.Title
                $spWeb.Dispose()
}
$spSite.Dispose()

It will display the name of site and title.