Friday, June 16, 2017

PowerShell to Add Data Row-wise to CSV File

Execute following script to insert data in new or existing CSV file.

$FilePath = "D:\Data\FileName,csv"
#Add Title Row
$Row = "Full Name,Age,Gender"
Add-Content ($Row) -Path $FilePath

#Add Data Row
$Name = "Prashant"
$Age = "29"
$Gender = "Male"
$Row = $Name + "," + $Age + "," + $Gender
Add-Content ($Row) -Path $FilePath

Create New Content Database Using SharePoint PowerShell

Execute following script to create new content database using SharePoint PowerShell. You can run this script on any WFE or APP server.

add-pssnapin Microsoft.SharePoint.PowerShell

#Set following parameters
$WebsiteName = "Test Site"
$DbServer = "SQLServer"
$WebAppUrl = "http://webapplink"
$DBName = "WSS_Content_" + $WebsiteName
$MaxSiteCount = 1 

$WarningSiteCount = 0

#Create new content database
New-SPContentDatabase -Name $DBName -DatabaseServer $DbServer -WebApplication $WebAppUrl -MaxSiteCount $MaxSiteCount -WarningSiteCount $WarningSiteCount -ErrorAction Stop

Create Site Collection in New Content Database in SharePoint 2010 / 2013

If you have a requirement where you need to Create a Site Collection in New Content Database, refer following PowerShell Script:

add-pssnapin Microsoft.SharePoint.PowerShell
$url = "http://sitecollection_link"
$ContentDatabase = "Content_DB_Name"
$WebsiteName = "New Site Collection Title"
$WebsiteDesc = "New Site Collection Description"
$Template = "STS#0" #It is for team site (use STS#1 for blank site)
$PrimaryLogin = "Domain\username1"
$SecondaryLogin = "Domain\username2"

New-SPSite -Url $url –ContentDatabase $ContentDatabase -Name $WebsiteName –Description $WebsiteDesc  -Template $Template -OwnerAlias $PrimaryLogin -SecondaryOwnerAlias $SecondaryLogin



Following script will create new content database and create site collection in newly created database. It is with parameters and also handles errors, so looks little longer :P


add-pssnapin Microsoft.SharePoint.PowerShell

#Set following parameters
$WebsiteName = "Test Site"

#Verify following parameters
$DbServer = "SQLServer"
$WebAppUrl = "http://webapplink"
#Uncomment if you are using managed path
#$ManagedPath = "path"
$DBName = "WSS_Content_" + $WebsiteName
$PrimaryLogin = "Domain\UserName"
$SecondaryLogin = "Domain\UserName"
$MaxSiteCount = 1
$WarningSiteCount = 0
$ContentDatabase = $null

#Create new content database
try
{
    $ContentDatabase = Get-SPContentDatabase $DBName -ErrorAction ignore
    if($ContentDatabase -eq $null)
    {
        New-SPContentDatabase -Name $DBName -DatabaseServer $DbServer -WebApplication $WebAppUrl -MaxSiteCount $MaxSiteCount -WarningSiteCount $WarningSiteCount -ErrorAction Stop
        Write-Host "Database Created Successfully" -ForegroundColor Green
        $ContentDatabase = Get-SPContentDatabase $DBName       
    }
    else
    {
        Write-Host "Database already exist." -ForegroundColor Red
    }


    #Set Parameters
    $url = $WebAppUrl + "/sites/" +$WebsiteName
    #Uncomment if you are using managed path
    #$url = $WebAppUrl + "/" + $ManagedPath + "/" +$WebsiteName
    $WebsiteDesc = "FieldDocs for year " +$WebsiteName
    $Template = "STS#1"

    #Create new site collection
    try
    {
        $site= $null
        $site = Get-SPSite $url -ErrorAction ignore   
       
        if($site -eq $null)
        {
            New-SPSite -Url $url –ContentDatabase $ContentDatabase -Name $WebsiteName –Description $WebsiteDesc  -Template $Template -OwnerAlias $PrimaryLogin -SecondaryOwnerAlias $SecondaryLogin -ErrorAction Stop
            Write-Host "Site Collection Created Successfully" -ForegroundColor Green
        }
        else
        {
            Write-Host "Site Collection already exist." -ForegroundColor Red
            $site.Dispose()
        }       
    }
    catch
    {
        Write-Host "Error in site collection creation" -ForegroundColor Red
    }
}
catch
{
    Write-Host "Error in database creation" -ForegroundColor Red
}