*Get local farm information using PowerShell
Using  the Get-SPFarm cmdlet, you can retrieve information, including  the name of the Config database, version number, and servers in the farm  that can be useful when creating scripts for your SharePoint farm.

You can open up the SharePoint 2010 Management Shell on your SharePoint server to view the settings of your farm and type:

PS > Get-SPFarm
You will  only see the name of the configuration database. You can get more detailed information if you pipe the cmdlet to the format list:

PS > Get-SPFarm | Format-List

* Quickly Access the SharePoint Hive

If you use SharePoint on a daily basis, you are probably familiar with the SharePoint hive. In SharePoint 2010, the hive is called the SharePoint root. The SharePoint root is used by SharePoint to store features, logs, binary files, templates and much more. It’s commonly used when performing administrative tasks in SharePoint. Changing location to the path is fun, the first time, but after typing the path 100 times, it gets a bit tedious. In Windows PowerShell you can solve this by adding a new PSDrive that points to the SharePoint Root.

New-PSDrive -Name SharePoint -Root “C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14” -PSProvider FileSystem

If you place the command in your profile it will always be loaded when you start PowerShell.

Now you can change location to the SharePoint Root using a simple command:

cd SharePoint:
If you want to open the folder in explorer, simply type:

explorer (Convert-Path SharePoint:)

 

* Find information about your SharePoint databases

You can use the Get-SPDatabase cmdlet to display information about the SharePoint databases.

PS > Get-SPDatabase

The example above displays all SharePoint databases. If you want to display a specific Database, use the identity parameter followed by the database ID.

PS > Get-SPDatabase -Identity

* Hide a list in SharePoint 2010

You can use Windows PowerShell to hide a list in SharePoint 2010. Simply store an instance of an SPList Object, set the Hidden property to $true and update the SPList object.

PS > $spWeb = Get-SPWeb http://sp01/site
PS > $spList = $spWeb.GetList(“http://sp01/site/Lists/MyList“)
PS > $spList.Hidden = $true
PS > $spList.Update()

* Remove a list in SharePoint 2010 from the Quick Launch toolbar

You can use Windows PowerShell to remove a list in SharePoint 2010 from the Quick Launch toolbar. Simply store an instance of an SPList Object, set the OnQuickLaunch property to $false, and update the SPList object.

PS > $spWeb = Get-SPWeb http://sp01/site
PS > $spList = $spWeb.GetList(“http://sp01/site/Lists/MyList“)
PS > $spList.OnQuickLaunch = $false
PS > $spList.Update()

* Enable folder creation on a list

You can enable folder creation on a list in SharePoint 2010 using PowerShell. Simply store an instance of an SPList Object, set the EnableFolderCreation property to $true, and update the SPList object.

PS > $spWeb = Get-SPWeb http://sp01/site
PS > $spList = $spWeb.GetList(“http://sp01/site/Lists/MyList“)
PS > $spList.EnableFolderCreation = $true
PS > $spList.Update()

* PowerShell commands for SharePoint 2010

SOS Member santhiswaroop

To find the PowerShell commands n SharePoint 2010, try these:

>Get-Command -Noun SPSite

You can get the all PowerShell commands:

>Get-Help Get-SPSite

You can get the Name, Syntax, Description, Related Links, Remarks

To Retrieve content database for a specific site collection:

>Get-SPContentDatabase -Site http://SPServer01

Results:

ID                  :96dfa345-43df-3edg-bbc6-1l4e8ee105le
Name                :WSS_content
Web Application     :SPWeb Application Name=Sharepoint – 80
Server              :SPServer01
CurrentSiteCount    :2

To retrieve the content database for a specific Web Application:

>Get-SPContentDatabase -WebApplication “Sharepoint – 80”

Results:

ID                  :96dfa345-43df-3edg-bbc6-1l4e8ee105le
Name                :WSS_content
Web Application     :SPWeb Application Name=Sharepoint – 80
Server              :SPServer01
CurrentSiteCount    :2

* Quickly Access the SharePoint Hive

If you use SharePoint on a daily basis, you are probably familiar with the SharePoint hive. In SharePoint 2010, the hive is called the SharePoint root. The SharePoint root is used by SharePoint to store features, logs, binary files, templates and much more. It’s commonly used when performing administrative tasks in SharePoint. Changing location to the path is fun, the first time, but after typing the path 100 times, it gets a bit tedious. In Windows PowerShell you can solve this by adding a new PSDrive that points to the SharePoint Root.

New-PSDrive -Name SharePoint -Root “C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14” -PSProvider FileSystem

If you place the command in your profile it will always be loaded when you start PowerShell.

Now you can change location to the SharePoint Root using a simple command:

cd SharePoint:
If you want to open the folder in explorer, simply type:

explorer (Convert-Path SharePoint:)

* Adding List Items using PowerShell

SharePoint 2010 offers over 500 new cmdlets that we can use to automate the SharePoint 2010 environment through Windows PowerShell. The cmdlets go as deep as SPWeb, everything beyond SPWeb requires additional scripting, such as adding fields, views and items. Here’s an example showing how to add an item to the Announcements list in SharePoint 2010:

$spWeb = Get-SPWeb -identity http://SP

$spList = $spWeb.GetList(“Lists/Announcements”)
$newItem = $spList.AddItem()
$newItem[“Title”] = “My First Announcement”
$newItem[“Body”] = “<h2>PowerShell Magic</h2>”
$newItem[“Expires”] = “5/5/2010”
$newItem.Update()
$spWeb.Dispose()

In the example we use the Get-SPWeb cmdlet to bind to a specific site. Next we use the GetList() method to retrieve a list. The method accepts a lists relative URL as input. We then use the AddItem() method to create a new item in SharePoint 2010. Finally we use the Dispose() method on the SPWeb object to make sure that the object is disposed of correctly.

* Managing sites in SharePoint 2010 Using PowerShell cmdlets

In SharePoint 2010 you can manage sites through Windows PowerShell. You can create new sites in a site collection using the New-SPWeb cmdlet:

New-SPWeb http://SP/TeamSite -Template “STS#0” –Name “Team Site” `
-Description “Description of Site” –AddToTopNav –UseParentTopNav

You can retrieve an existing site using the Get-SPWeb cmdlet:

Get-SPWeb http://SP/TeamSite

You can configure a site through the Set-SPWeb cmdlet:

Get-SPWeb http://SP/TeamSite | Set-SPWeb -Description “New Description”

And finally, you can remove a site using the Remove-SPWeb cmdlet:

Remove-SPWeb http://SP/TeamSite -Confirm:$false

Setting the –Confirm switch parameter to $false omits the confirmation returned from PowerShell.

Backup and Restore Site Collections in SharePoint 2010 using PowerShell

Backup-SPSite http://SP –Path C:BackupsiteCollection.bak

Restoring a site collection in SharePoint 2010 is just as simple:

Restore-SPSite http://SP –Path C:BackupsiteCollection.bak

* User Management in SharePoint 2010 with PowerShell

SharePoint 2010 includes three cmdlets that you can use to manage users in SharePoint 2010. Here’s an example on adding a new user:

New-SPUser “XYZdavid” -Web http://SP -PermissionLevel “Contribute”

 

The example above adds the user “david” to the specified site and sets the user permissions to contribute.

You can retrieve an existing user through the Get-SPUser cmdlet:

Get-SPUser “XYZdavid” –Web http://SP

 

If you pipe the object to the Format-List cmdlet you can list all properties.

Get-SPUser “XYZdavid” –Web http://SP | Format-List *

 

You can modify users through the Set-SPUser cmdlet:

Set-SPUser “XYZdavid” -Web http://SP -Email mail@XYZ.com

 

And finally, you can remove a user through the Remove-SPUser cmdlet:

Remove-SPUser “XYZdavid” -Web http://SP

* Add Managed Paths to Web Applications using PowerShell

You can use the New-SPManagedPath cmdlet to add managed paths to a given Web application:

PS > New-SPManagedPath -RelativeURL MyPath `
>> -WebApplication http://SP01.powershell.nu

The example above creates a “MyPath” managed path for the Web application http://SP01.powershell.nu.

* Adding Alternate Access Mappings using PowerShell

Adding Alternate Access Mappings is a simple task using Windows PowerShell. You can simply use the New-SPAlternateUrl cmdlet, which supports the creation of internal or public URLs for a given Web application. The examples below demonstrate how to create both a public and internal URL:

# Public
PS > New-SPAlternateURL -Url http://public.powershell.nu `
>> -Zone “Internet” -WebApplication http://sp01.powershell.nu

# Internal
PS > New-SPAlternateURL -Url http://internal.powershell.nu `
-Zone Default -WebApplication http://sp01.powershell.nu -Internal

* Adding Managed Accounts using PowerShell

You can add SharePoint 2010 managed accounts by using New-SPManagedAccount in Windows PowerShell. When creating automated scripts, you may want to check if an account is already a managed account. Simply use Get-SPManagedAccount and a simple if/else statement to determine if the managed account already exists. Here’s a short code example:

$userName = “domainuser”
if(Get-SPManagedAccount | Where-Object { $_.UserName -eq $userName }){
# Managed Account Already exists
Write-Host “Managed Account: $userName exists”
} else {
# Get User Credentials
$credential = Get-Credential -Credential $userName
# Create New Managed Account
New-SPManagedAccount -Credential $credential
}

* Adding Solutions

Did you know that when you upload solution packages to a farm, you can use the Add-SPSolution cmdlet? Simply:

# Adding a Solution
Add-SPSolution –LiteralPath C:StuffMySolution.wsp
After you’ve uploaded a solution package, you can use the Install-SPSolution cmdlet. The example below will get you all solutions and then filter out the solution where the name is equal to MySolution.wsp. Finally, the solution is installed using the Install-SPSolution cmdlet.

PS > Get-SPSolution | Where-Object { $_.Name -eq “MySolution.wsp” } |
>> Install-SPSolution –GACDeployment

* Changing Custom Master Page

You can use Windows PowerShell to change a Web’s custom master page. Simply point the CustomMasterUrl to a new master page:

PS > $spWeb = Get-SPWeb http://SP01.powershell.nu
PS > $spWeb.CustomMasterUrl = “/_catalogs/masterpage/myNew.master”
PS > $spWeb.Update()
PS > $spWeb.Dispose()

* Create Default Associated Groups

When creating Web site using PowerShell, you may encounter situations where you’re missing the site’s default groups. A simple solution to this is using the CreateDefaultAssociatedGroups method. The example below demonstrates how you can use the method:

PS > $spWeb = Get-SPWeb http://SP01.powershell.nu
PS > $spWeb.CreateDefaultAssociatedGroups(“powershellspAdmin”,””,””)
PS > $spWeb.Update()
PS > $spWeb.Dispose()

* Changing Locale

You can change a Web site’s locale setting using Windows PowerShell. Simply replace the value of the locale property by using an object of the type System.Globalization.CultureInfo. The example below shows how to set the locale to Swedish:

PS > $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture(“sv-SE”)
PS > $spWeb = Get-SPWeb http://SP01.powershell.nu
PS > $spWeb.Locale = $culture
PS > $spWeb.Update()
PS > $spWeb.Dispose()

* Enable Auditing

You can enable site audit using Windows PowerShell. The example below demonstrates how to add specific audit flags on a site collection and enable auditing:

PS > $spSite = Get-SPSite http://SP01.powershell.nu
PS > $spSite.Audit.AuditFlags =
>> ([Microsoft.SharePoint.SPAuditMaskType]::All `
>> -bxor[Microsoft.SharePoint.SPAuditMaskType]::View)
PS > $spSite.Audit.Update()
PS > $spSite.Dispose()

* Handling Errors in Your Scripts
When creating automated SharePoint scripts using Windows PowerShell, it’s a good idea to add some form of error handling. A typical example is when you try to access a non-existent site collection. By default, Windows PowerShell displays an error message. You can use a Try/Catch statement if you want to handle this. Note that the –ErrorAction is set to stop on the cmdlet:

$url = “http://SP01.powershell.nu
try {
# Trying to get the Site Collection
# ErrorAction is set to Stop
Get-SPSite $url -ErrorAction Stop
}
catch {
# If the Command in the Try block fails,
# The code in the Catch block is executed
Write-Host “That didn’t work..”
}

* Removing Nodes from the QuickLaunch

You can use Windows PowerShell when removing one (or more) nodes from a Web site’s QuickLaunch. The example below shows  how you can remove everything except for nodes under the lists heading:

PS > $spWeb = Get-SPWeb http://SP01.powershell.nu -ErrorAction Stop
PS > foreach($node in ($spWeb.Navigation.QuickLaunch |
>> Where-Object { $_.Title -ne “Lists” })) {
>> $node.Delete()
>> }
PS > $spWeb.Dispose()

* Remove Group Permissions
You can remove a group’s permissions on a specific Web site with  Windows PowerShell. Simply remove the group from the role assignment collection:

PS > $spWeb = Get-SPWeb http://SP01.powershell.nu
PS > $spPrincipal = [Microsoft.SharePoint.SPPrincipal]$spWeb.SiteGroups[“Site Members”]
PS > $spWeb.RoleAssignments.Remove($spPrincipal)
PS > $spWeb.Dispose()

Source:
http://secretsofsharepoint.com/cs/blogs/tips/default.aspx