PowerShell Cheat Sheet Reference Quick Guide

My learning area                                  Get full cheat sheet is from here

Here are my Farm Setup commands                 Josh Gavant Blog          Active Directory Quick Start Guide & here

Getting Started

Start PowerShell ISE
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell
Set-location $home

Script Signing
Set-ExecutionPolicy Unrestricted

PowerShell Admin Access to SharePoint
Add-SPShellAdmin -username nsa\nsaspsetup
Remove-SPShellAdmin –username nsa\nsafarmadmin

Getting Help Simple Way
Get-Command *project*    this finds the commands name
Get-Help  <copy and paste full command name from above step>
or do it this way
Get-Command –Module Microsoft.SharePoint.PowerShell –noun SPWebApplication or –verb backup

Get all properties and methods of an object
Get-SPWebApplication http://sharepoint | Get-Member | more  #more shows all on screen
(Get-SPFarm).BuildVersion    or    (Get-SPWebApplication http://sharepoint).Id
Get-SPFarm | Format-Table -Property DisplayName, Status, BuildVersion
(Get-SPWeb http://sp.sp.local).lists | Sort-Object Title | select Title, Id, ItemCount, hasUniqueRoleAssignments, EnabledAttachments, EnableThrottling | Format-Table
Get-SPDatabase | Select Name, Status

To be more performant use the –filter property, it executes on the server
Get-SPSite | Get-SPWeb –Filter {$_.Template –eq “STS#1”}

Control+C will exit the current processing and return you to your prompt

Note: Single-line commands will properly handle the disposal of SharePoint objects

To allow for farm credentials with popup, put in brackets ? FarmCredentials (Get-Credential)

Get-SPFarm #When you type this and press enter, it will give you the farm name and status whether it is online or offline

Get-spwebapplication –includecentraladministration #This will give display name of list of web applications as well as the name of the central administration and there corresponding URL.

Install-SPWebTemplate -path "template file path" -name "My Custom Template" -description "Test site template"

Get-SPServer #It will return the list of servers.

Get-SPLogEvent -MinimumLevel "Warning" #Returns results from a Unified Logging Service (ULS) trace log

Get-SPDatabase #It will give us all the properties of database.

Get-SPDatabase –ServerInstance “sp1\sp1admin”

$env:computername  -  Gets current server name

Formatting

Write 3 blank  lines    Write-Host "`n`n`n"
Write 20 blank lines        for ($i = 1; $i -lt 21; $i++) {write-host}

`  word-wrap symbol

`t  tab

`n  new line

`r  carriage return

`a  Alert

`b  Backspace

`0  Zero or Null

`’  Speech mark single quote

`" speech mark  double quote

Working with Sites and Lists Hey scripting guy

To get the number of site collections:
(Get-SPSite).Count

To remove all site collections (if they do not have subsites): without confirmations
Get-SPSite | Remove-SPSite -Confirm:$false

To create a new site collection where we find Template based on name:
New-SPSite $url -OwnerAlias $admin -Template (Get-SPWebTemplate
| Where {$_.Title -eq "Team Site" } )

To create new web or sub/site:

New-SPWeb $url -Template (Get-SPWebTemplate | Where {$_.Title –
eq "Team Site" })

To create new task list in all sites:

Get-SPWeb | ForEach {$_.Lists.Add("My Tasks", "",
$_.ListTemplates["Tasks"])}

To create new task list in site:

Get-SPWeb $url | ForEach {$_.Lists.Add("My Tasks", "",
$_.ListTemplates["Tasks"])}

To enumerate available workflows:

Get-SPWeb $url | Select -Expand WorkflowTemplates | Select Name

To enumerate all document libraries in your site:

Get-SPWeb $url | Select -Expand Lists | Where {$_.BaseType -eq
"DocumentLibrary"}

Get a specific list and some properties
(Get-SPWeb http://sharepoint/team/blog).lists[“Links”] | Format-Table Title, Id,
ItemCount, hasUniqueRoleAssignments, EnabledAttachments, EnableThrottling

To backup many sites
Get-SPWebApplication | Get-SPSite | ForEach-Object{$FilePath = “c:\backups\” +
$_.Url.Replace(“http://”,”“).Replace(“/”,”-”) + “.bak” ; Backup-SPSite –Identity $_.Url -Path $FilePath}

To update the portal site connection you can:
SiteCollection.PortalName = "Contoso Home";
SiteCollection.PortalUrl = http://www.consoto.com;

Working with Content
Show all items in a site:
Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | select
Name, Url

Show only documents:

Get-SPWeb $url | Select -Expand Lists | Where {$_.BaseType -eq
"DocumentLibrary"} | Select -Expand Items | select Name, Url

Search for item:

Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | Where
{$_.Name -like "*.doc"} | select Name, Url

Get a listing of all lists on a web
(Get-SPWeb http://sharepoint/team/blog).Lists

Get Id of List Template
Get-SPWeb http://sharepoint/team/blog).ListTemplates | Where-Object {$_.Name –eq
“Links”}

Create a new List
(Get-SPWeb http://sharepoint/team/blog).Lists.Add(“Demo Links List”, “This is the description”, 103)

Delete a list
$listId = (Get-SPWeb http://sharepoint/team/blog).lists[“Demo Links List”].Id (Get-SPWeb http://sharepoint/team/blog).lists.Delete($listId)


To create a new document in a document library:

function New-SPFile($WebUrl, $ListName, $DocumentName,
$Content)
{
$stream = new-object System.IO.MemoryStream
$writer = new-object System.IO.StreamWriter($stream)
$writer.Write($content)
$writer.Flush()
Get-SPWeb $WebUrl | ForEach {$_.Lists[$ListName]} | ForEach
{$_.RootFolder.Files.Add($DocumentName, $stream,
$true);$_.Update()}
}
New-SPFile -WebUrl "http://mycompany/sites/mysite" -ListName
"Shared Documents" -DocumentName "MyFirstDocument" -Content
"Power Blues"

Object disposal
By default, SharePoint PowerShell tends to dispose the objects at the end of the
pipeline. This means that variables assigned to pipeline output might lose data
once the one-liner is finished.
If you need the data to persist in memory and want to make SharePoint
PowerShell store results beyond one-liners run:
Start-SPAssignment
To get back to default behavior (dispose once the pipeline is finished):
Stop-SPAssignment

Format Dates tip from Microsoft here

Working with Hash tables

Using the Switch Statement

Get Child item fun

Reading Text Files

Reverencing Variables

Functions

Strings   and here

Range operator

 

 

Cheat Sheet powershell_reference_1

Cheat Sheet powershell_reference_2

Cheat Sheet powershell_reference_3

Cheat Sheet powershell_reference_4