Friday 30 October 2015

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

Set-ExecutionPolicy : 
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. At line:1 char:20 
+ set-ExecutionPolicy <<<<  unrestricted
              + CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException 
              + FullyQualifiedErrorId :           System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

Solution: 
1) This error is often displayed because you are not executing the command 
as an administrator.

To solve this issue, try the following.
Right-click or hold Shift and select Windows PowerShell > Run as administrator.

If the above approach doesn't resolve then try step 2
2)Run: regedt32 
* Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell
* Right-Click > Permissions 
* Select, and add your account, grant it "Full Control" privileges as shown in below figure


Unlock Site Collections when locked often during Backup and Restoring the Sites.

Add-PSSnapin Microsoft.SharePoint.PowerShell 

$Admin =  New-Object 
Microsoft.SharePoint.Administration.SPSiteAdministration(‘http://server:port/’site)

$Admin.ClearMaintenanceMode() 

Send HTML Email through Powershell Using Microsoft.SharePoint.Utilities

Add-PSSnapin Microsoft.Sharepoint.Powershell

$headers = New-Object System.Collections.Specialized.StringDictionary

$headers.Add("to", "asishm@domain.com")       

$headers.Add("from", "system@domain.com")

$headers.Add("subject", "Task Details")

$headers.Add("content-type", "text/html")

$bodyText = "Hello <br>" 

$bodyText += "Following are the Task details : <br><br>" 


$bodyText +="<br>Thank and Regards,<br>"
$bodyText +="Asish,<br>"
$bodyText +="Company"

[Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($SPWeb, $headers, $bodyText)

Get Version Details of a Particular Item in a Sharepoint List.

# Get each version details of a particular Item in a List/Library
# To Know the value in a custom column when the particular version was created
# For e.g Lets say there is custom column named 'User' in a list. 
# And each time when the particular item is Modified by different user then the 'User' column  also get updated  per each version.
# Such as for Version 1.0=====> User: Mr. ABC
#                    Version 2.0=====> User: Mr. XYZ
#                    Version 3.0=====> User: Mr. DEF
# So this code helps to retrieve the value in 'User' Column

Add-PSSnapin Microsoft.Sharepoint.Powershell

$WebUrl = “http://server:port/Site”
$SPWeb = Get-SPWeb -Identity $WebUrl
$SPList = $SPWeb.Lists["List Name"]
$SPItem = $SPList.Items | Where-Object {$_['ID'] -eq "100"}
# Counts the number of Versions of an item
$VerCount = $SPItem.Versions.Count;
# Declare an array
$Test = @(0..$VerCount)
# Create a dictionary object
$dictionary = New-Object 'System.Collections.Generic.Dictionary[String,String]' 
# Iterate through each version of an item
foreach($Ver in $SPItem.Versions)
{
    if($VerCount -gt 0)
    {
        $VersionLabel = $Ver.VersionLabel 
        $CustomColVal = $Ver.Item("Custom Column"
        $User = $UploadUser
        $dictionary.Add($VersionLabel,$CustomColVal)
    }
}
return $dictionary

Import Items from CSV and Export to Sharepoint List.

Add-PSSnapin Microsoft.SharePoint.PowerShell

# Import the .csv file, and specify manually the headers, without column name in the file 
$contents = Import-CSV "D:\Test\List.csv" -header("Title", "SiteURL", "CreatedBy"-delimiter ';'

# Web URL
$webURL = “http://server:port/Site”
$web = Get-SPWeb -Identity $webURL
$listName = "List Name"
$list= $web.Lists["$listName"] 

# Iterate for each list column
$count = 0
foreach ($row in $contents )
{  
    $count++
    $item = $list.Items.Add();
    $item["Title"] = $row.Title
    $item["URL"] = $row.SiteURL
    $item["Created By"] = $row.CreatedBy
    $item.Update()
}
Write-Host -ForegroundColor green "List Updated Successfully"
$web.Dispose()

Export Sharepoint List Items to CSV.

 Add-PSSnapin Microsoft.SharePoint.PowerShell

$SPWeb = Get-SPWeb “http://server:port/Site"
$SPList = $SPWeb.Lists[“List Name”]
$exportlist = @()
$SPList.Items | foreach {
      $obj = New-Object PSObject -Property @{
      ####”CSV Column Name” – “SharePoint List Column”
       “Title” = $_[“Title”]
       “SiteURL” = $_[“URL”]
       “CreatedBy” = $_[“Created By”]
       }
    $exportlist += $obj
   
    }
$exportlist | Export-Csv -path "D:\Shares\ListBackUp.csv"
    
$SPWeb.Dispose()