Showing posts with label Powershell Script. Show all posts
Showing posts with label Powershell Script. Show all posts

Thursday, March 16, 2017

sharepoint Powershell migrate users in a web application

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
param([string]$url = "http://sharepointsite/", `
[string]$oldprovider = `
"keystoneuser", `
[string]$newprovider = `
"i:0ǵ.t|keystone sts|radiant\")

add-pssnapin microsoft.sharepoint.powershell -EA 0

# Get all of the users in a site
#$users = get-spuser -web $url -Limit "11000"
$csvFile = "C:\Users\Users.csv"
# Loop through each of the users in the site
Import-Csv $csvFile | ForEach-Object{
# Create an array that will be used to split the user name from the domain/membership provider
$a=@()
$username = $_.oldlogin
$a = $username.split(":")
$userloginprovider = $a[0]
$displayname = $a[1]
Write-Host "Old Username: $username."
# Create the new username based on the given input
$newalias = $newprovider + $displayname
$user = Get-SPUser -Identity $username -Web http://
sharepointsite/ -EA "SilentlyContinue"
if($user -notlike "")
{
Move-SPUser –Identity $user -NewAlias "$newalias" -IgnoreSID -Confirm:$false -EA "SilentlyContinue"
Write-Host "User Changed to $newalias"
}
}
}); 




To migrate user in farm use the following,


$farm.MigrateUserAccount( $_.oldlogin, $_.newlogin, $false )

Monday, February 27, 2017

PowerShell - Event Receivers on SharePoint List


There are times when you want to check which event receivers are attached to a SharePoint list.   This can be easily achieved through use of a PowerShell script.
$spWeb = Get-SPWeb Identity http://sharepoint
$spList = $spWeb.Lists["My List"
$spList.EventReceivers | Select Name, Assembly, Type
There are also times when you might want to remove any event receivers from the list
$spWeb = Get-SPWeb Identity http://sharepoint
$spList = $spWeb.Lists["My List"
$spList.EventReceivers | Select Name, Assembly, Type

$eventsCount = $spList.EventReceivers.Count

for ($i = $eventsCount-1; $i -ge 0; $i--)
{
    $spList.EventReceivers[$i].Delete()
}

$spList.Update()

SharePoint 2010 - PowerShell Content Database Backup


“This post details the steps for backing up the content database of a SharePoint site through PowerShell.   view the steps for backing up a content database through Central Administration.”
PowerShell provides a means to backup data within SharePoint through the following command:
Backup-SPFarm -Directory [Path] -BackupMethod [Full|Differential] -Item [BackupItem] -Verbose -Percentage [Percentage]
Further information on each of the parameters in this command are outlined bellow.


Directory

The Directory parameters specifies the path where the backup will be generated.  This can either be a UNC path or a location on the server.   “You don’t need to enter the spbr* folder name as this will be auto generated”
BackupMethod
The backup method parameter details the type of backup that will be performed.  This can either be Full or Differential:
  • Full - backs up the selected content with all history
  • Differential - backs up all changes to the selected content since the last full backup
  • Item

    The item parameter specifies the component within the farm that will be backed up.  If the item has a unique name you can use just that, however, if it doesn’t then you will need to us the path to the item within the farm.In order to get the path to the item you can use the following command:
    Backup-SPFarm -ShowTree
    This will list the tree structure of items within the farm.  In my experience the path to a content database within the farm is in the following format:
    “Farm\Microsoft SharePoint Foundation Web Application\[Site Name]\[Content Database Name]”

    Verbos

    This parameter is optional.  The verbose parameter provides more information about the operation being run.

    Percentage

    This parameter is optional.  The percentage parameter can be used to report progress about the operation at defined percentage intervals.

    Friday, December 2, 2016

    Delete all files & folders from SharePoint library

    Add-PSSnapin Microsoft.SharePoint.PowerShell

    # Replace siteurl with actual web url
    $web = Get-SPWeb -Identity "siteurl"
    # Replace docurl with document library url
    $list = $web.GetList("docurl")

    function DeleteFiles {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            # Delete file by deleting parent SPListItem
            Write-Host("DELETED FILE: " + $file.name)
            $list.Items.DeleteItemById($file.Item.Id)
        }
    }

    # Delete root files
    DeleteFiles($list.RootFolder.Url)

    # Delete files in folders
    foreach ($folder in $list.Folders) {
        DeleteFiles($folder.Url)
    }

    # Delete folders
    foreach ($folder in $list.Folders) {
        try {
            Write-Host("DELETED FOLDER: " + $folder.name)
            $list.Folders.DeleteItemById($folder.ID)
        }
        catch {
            # Deletion of parent folder already deleted this folder
        }
    }

    Monday, October 24, 2016

    Fast Site Creation in SharePoint 2016

     Fast Site Collection Creation for a template,

    Enable-SPWebTemplateForSiteMaster -Template “STS#0” -CompatibilityLevel 15


    Let’s get the DB where we have already provisioned a site collection.
    $DB = Get-SPContentDatabase -site http://peakfinders/sites/fast
    Now lets create a SiteMaster in it.
    New-SPSiteMaster -ContentDatabase $DB -Template “STS#0”

    Get-SPSiteMaster $DB

    Open Central Administration >View all the site collections


    Remove-SPSiteMaster to remove the SiteMaster from your Content database.
    Create new site collection using –CreateFromSiteMaster parameter.
    New-SPSite http://sp2016vm:4430/sites/FastSite1 -ContentDatabase $DB -CompatibilityLevel 15 -CreateFromSiteMaster -OwnerAlias “sp2016\sp16_farm”