Useful powershell snippets

31/07/2012

I just wanted to share a list of useful powershell commands that I use every once in a while, the list will most likely keep growing. I have divided it into two sections one for SharePoint related commands and one for just plain old ps commands.

Powershall Commands
Add-PSSnapin “Microsoft.SharePoint.Powershell” – Yes this will add the sharepoint commands, very useful, if you are working with sharepoint.
gc env:computername – this returns the computer name, somewhat useful in scripts now when the good old batch command %COMPUTERNAME% doesn’t work.

SharePoint:
$site = Get-SPSite http://yoursite
[Microsoft.SharePoint.Administration.SPWebService]::ContentService.WebApplications[$site.WebApplication.ID].WebConfigModifications

SharePoint deployment:
$path = Get-location
$path = join-path $path Package.wsp
Add-spsolution $path

To check installation status:
get-spsolution | where {$_.Name -like “*package.wsp”}

Enable developer dash board

$sp2010 = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;
$sp2010.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::OnDemand;
$sp2010.RequiredPermissions = 'EmptyMask';
$sp2010.TraceEnabled = $true;
$sp2010.Update();

To enable the developer dashboard, open the SharePoint 2013 Management Shell and type the following:
[powershell]
$content = ([Microsoft.SharePoint.Administration.SPWebService]::ContentService)
$appsetting = $content.DeveloperDashboardSettings
$appsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::On
$appsetting.Update()
[/powershell]

To disable the developer dashboard, open the SharePoint 2013 Management Shell and type the following:

[powershell]
$content = ([Microsoft.SharePoint.Administration.SPWebService]::ContentService)
$appsetting = $content.DeveloperDashboardSettings
$appsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::Off
$appsetting.Update()
[/powershell]