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”


    Wednesday, September 21, 2016

    How to move Child Subsite to another site collection using PowerShell


    Add SharePoint snapin

    Add-PSSnapin Microsoft.SharePoint.PowerShell –ea SilentlyContinue

    Set variables - FYI exportfolder will throw an error if folder is already created.

    $exportfolder = "E:\backup\NAME" $exportfile = "\s.cmp" $exportsite = "http://test/hr/site 101" $exportlocation = $exportfolder+$exportfile $importlocation = "http://test2/hr/site 101"

    Get export site's template

    $web = Get-SPWeb $exportsite $webTemp = $web.WebTemplate $webTempID = $web.Configuration $webTemplate = "$webTemp#$webTempID" $web.Dispose()

    Create export folder

    $null = New-Item $exportfolder -type directory

    Export site

    Export-SPWeb $exportsite –Path $exportlocation -IncludeUserSecurity -IncludeVersions 4 Write-host "$exportsite has been exported to $exportlocation"

    Create new site ready for import

    $null = New-SPWeb $importlocation -Template "$webTemplate" Write-host "$importlocation created ready for import"

    Import site

    Import-SPWeb $importlocation –Path $exportlocation -IncludeUserSecurity –UpdateVersions 2 Write-host "$exportsite has been imported to $importlocation" -foregroundcolor "Green"

    Friday, September 16, 2016

    How to get attachments from SharePoint list with PowerShell?

    SharePoint 2013 it works

    $webUrl = "http://peakfinder/"    
    $library = "Product Information"
    $spSite = new-object Microsoft.SharePoint.SPSite($webUrl)
    $w = $spSite.OpenWeb()
    $l = $w.Lists[$library]
    $resultHashtable = @{}

    foreach ($listItem in $l.Items)
    {     
        Write-Host "    Content: " $listItem.ID 
        foreach ($attachment in $listItem.Attachments)
        {
            $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)
            $linkAttachment = "http://peakfinder/" + $file.ServerRelativeUrl
            Write-Host "http://peakfinder/"$file.ServerRelativeUrl
            if($linkAttachment){
            $resultHashtable.Add($listItem.ID, $linkAttachment)
            }    
        }    
    }
    #Export CSV
    $resultHashtable.GetEnumerator() | Sort-Object -Property Name -Descending |
    Select-Object -Property @{n='SiteURL';e={$_.Name}},Value |
    Export-Csv -Path Attachments.csv -NoTypeInformation

    How to enable the officewebapps in Sharepoint 2013 using Powershell Script

    Get-OfficeWebAppsFarm

    Powershell Script
    $owaFarm = Get-OfficeWebAppsFarm;
    $owaFarm .OpenFromUrlEnabled = $true;

    Tuesday, July 19, 2016

    Find all users who have not added a picture to their Mysite Profile

    [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server") | out-null
    $site=new-object Microsoft.SharePoint.SPSite("https://peakfinders") #Central Admin site
    $servercontext=[Microsoft.Office.Server.ServerContext]::GetContext($site)
    $site.Dispose() # clean up
    $upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($servercontext)
    $pc = $upm.GetEnumerator()
    foreach($p in $pc){$v = $p["PictureUrl"].Value;if($v -eq $null){Write-Host $p["AccountName"].ToString()"doesn's have a picture set."}}

    #Optional, display all the Picture URL's
    $pc = $upm.GetEnumerator();
    foreach($p in $pc){$p["PictureUrl"].Value}

    Thursday, March 17, 2016

    WSP gets stuck in "Deploying"


    • stsadm -o execadmsvcjobs on the server running Central Admin
    • Install-SPSolution has only the -Force, 
    • Check for Sharepoint 2013 Timer service






    Add-SPSolution –LiteralPath "d:solution.wsp" 

    install solution and we executed the following command:

    Install-SPSolution -Identity "solution.wsp" -WebApplication http://peakfinders.com/  –GACDeployment

    Import subsite and Site Collection in SharePoint power shell script

    Import the subsite

    STSADM:

    stsadm -o import -url "http://sharepointsite/sites/order/" -filename D:\backup.cmp -includeusersecurity -versions 4

    Powershell:

    Add-pssnapin microsoft.sharepoint.powershell

    Export-SPWeb -identity "http://sharepointsite/sites/order/"  -path D:\backup.cmp -includeusersecurity  -force

    Export the Subsite moved to site Collection Using PowerShell

    Export the subsite


    STSADM:

    stsadm -o export -url "http://sharepointsite/sites/order/" -filename D:\backup.cmp -includeusersecurity -versions 4


    Powershell:


    Add-pssnapin microsoft.sharepoint.powershell

    Import-SPWeb -identity "http://sharepointsite/sites/order/"  -path D:\backup.cmp -includeusersecurity  -force

    Tuesday, March 15, 2016

    How to get all default values for a document library

    $FieldName = $list.Fields["NH Department"]
    Write-Output $("NH Department - Default Value " + $FieldName.DefaultValue)

    Monday, March 7, 2016

    How to get specific sub sites Using Get-SPWeb object

    $WepApp = "http://peakfinderspowe/"
    $site = Get-SPSite $WepApp
    $array = @("Title of the First Site", "Title of the Second Site")
    $filteredWebs = $site | Get-SPWeb | ? {$array -contains $_.Title}

    How to know List installed products & components using powershell

    SharePoint products using PowerShell,



    $listApps=Get-WmiObject -Class Win32_Product | Where {$_.IdentifyingNumber -like “*90150000-*”}
    $listApps | Sort -Property Name | ft -Autosize

    Central Admin. Simply navigate to your SharePoint Central Administration site and on the main page under Upgrade and Migration, click on "Check product and patch installation status


    Wednesday, March 2, 2016

    How to create site collections using SharePoint PowerShell 2013


    Declare variable for template:

    $template = Get-SPWebTemplate "STS#0"



    New-SPSite -Name "VSPServer Intranet" -Url "http://peakfinders" –HostHeaderWebApplication $hnscWebApp -Template $template -OwnerAlias "Peakfinder\admin"

    Tuesday, February 23, 2016

    Restore List Items from Recycle bin using Powershell Script by having item Guid

    $site = Get-SPSite http://server/sites/site
     $site.RecycleBin.Restore($DeletedItem)
    restore a collection of items

     Assuming $DeletedItems is a collection of GUIDs:


     ForEach ($deletedGuid in $DeletedItems)
     {
     $site.RecycleBin.Restore($deletedGuid) 
    }

    Friday, February 19, 2016

    Import\ Export SharePoint Subsite or SPWeb Using Powershell

    In this article,
    Run PowerShell as Administrator.

    Export Subsite:

    export-spweb -identity "http://peakfinders/dara"  -path D:\sitebackup.cmp -includeusersecurity 

     Import Subsite:

    import-spweb -identity "http://peakfinders/dara1"  -path D:\sitebackup.cmp -includeusersecurity  -force

    Monday, February 15, 2016

    SharePoint Auditing logs?

    1. Site Settings –> Site Collection Administration, 
    2. There was no link avaliable in to "View Audit Logs" 
    3. When we creating the following templates were used to create the default site: 

    1. Document Workspace
    2. Wiki
    3. Blog
    4. Records Center
    We have to run PowerShell script for this enable below mention script:

    stsadm -o activatefeature -name Reporting -url http://mysite/ -force
    This activated the feature

    Saturday, February 13, 2016

    Add, Update and install WSP in SharePoint using PowerShell Script

    Add WSP using the following power shell script
    $wspname="C:\\name.wsp"
    Add-SPSolution -Identity wspname

    Upgrade a WSP using the following power shell script.

    (Update-SPSolution -Identity wspname -LiteralPath Physicalpath -GACDeployment)

    Update-SPSolution -Identity ListDefinition.wsp -LiteralPath 


    C:\\name.wsp -GACDeployment 

    Install the newly created Feature using powershell script.

    (Install-SPFeature FeatureFolderName)

    Install-SPFeature "ListDefinition_New_List_Feature"