Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

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
}


Saturday, October 5, 2013

PowerShell Popup

To show a popup message in PowerShell, we need to create an object of wscript.shell and then call popup method with parameters.

Popup Syntax
popup("Message to be displayed",0, "popup window title", button type id + icon style id)

Example
$alert = new-object -comobject wscript.shell
$answer = $alert.popup("Do you want to delete these files?", 0,"Delete Files",4+32)
If ($answer -eq 6) {
  $alert.popup("You answered yes.")
} else {
  $alert.popup("You answered no.")
}

#Button Types 
#0 Show OK button
#1 Show OK and Cancel buttons
#2 Show Abort, Retry, and Ignore buttons
#3 Show Yes, No, and Cancel buttons
#4 Show Yes and No buttons
#5 Show Retry and Cancel buttons

#icon style
#16 Stop Icon
#32 Question Icon
#48 Exclamation Icon
#64 Information Icon

#return values of popup method
OK = 1
Cancel = 2
Abort = 3
Retry = 4
Ignore = 5
Yes = 6
No = 7

Delete All SharePoint List Items Using PowerShell 2010

How to delete all list items from SharePoint list?

Follow the steps to delete all list items from SharePoint List

1. Create new PowerShell (.ps1) file with below script using Notepad

$Url = "http://site-url:5000"
$ListName = "Sales 2013"
$Web = Get-SPWeb $Url
$List = $Web.lists[$ListName]

if($List -eq $null)
{    
  Write-Error "The List cannot be found";return
}

Write-Warning "Deleting all list items from $($ListName)"
$Items = $List.GetItems()
Write-Host "Total Items to be deleted : $($Items.count)"

if($Items.count -gt 0)
{
  $shell = new-object -comobject wscript.shell
  $result = $shell.popup("Total Item to be deleted : $($Items.count) `nDo   you want to continue?",0,"Alert",4+32)

if($result -eq "6")
{
foreach ($item in $Items)
{
  $itemId = $item.ID
  $List.GetItemById($itemId).Delete()
  Write-Host "Deleted list item with id $($itemId)"
}
}
}
$List.Update()
$Web.Dispose()


2. Replace values of parameters $Url and $ListName

3. Save file in server hard drive (Example: D:\PowerShell\Delete-List-Items.ps1)

4. Open "SharePoint 2010 Management Shell" with "Run as administrator"

5. Navigate to the folder where the script  file is stored
    (Example: cd D:\PowerShell)

6. Select file to be executed
    (Example: .\Delete-List-Items.ps1)

7. Press enter and enjoy!!

Wednesday, July 3, 2013

SharePoint Web Id using PowerShell

Get SharePoint Web Id using PowerShell


1. Open SharePoint 2010 management Shell (PowerShell) with administrator privileges

2. Execute following command


$site =  Get-SPSite "http://yoursite"
$web = $site.AllWebs["Web Display Name"]
#For root web
#$web = $site.RootWeb
$web.ID

3. Above script will show unique id of the web

Wednesday, March 20, 2013

Empty Site Collection Recycle Bin using SharePoint 2010 Management Shell (PowerShell)

Follow the steps to Empty Site Collection Recycle Bin using SharePoint 2010 Management Shell (PowerShell):
1.   Open SharePoint 2010 Management Shell with Run as administrator

2.   Modify Site collection Url in following script and execute:

$sitecollectionUrl = "http://site:port/sitecollection"

$siteCollection = New-Object Microsoft.SharePoint.SPSite($sitecollectionUrl)
write-host("Items to be deleted : " +$siteCollection.RecycleBin.Count.toString())
$now = Get-Date
write-host("Deleting started at " +$now.toString())
$siteCollection.RecycleBin.DeleteAll();
$now = Get-Date
write-host("Deleting completed at " +$now.toString())
$siteCollection.Dispose(); 

Contact Me for any technical help related with SharePoint 2010.

Tuesday, March 19, 2013

Create new SharePoint List Item using SharePoint 2010 Management Shell (PowerShell)

Follow the steps to create new SharePoint List Item using SharePoint 2010 Management Shell (PowerShell):
1.       Open SharePoint 2010 Management Shell with Run as Administrator

2.       Modify and execute following script:

#Web absolute URL where list exists
$webURL = "<http://site:port/site/subsite>" 

#SPList Name where items to be added
$listName = "<List Name>" 

#Get the SPWeb object and save it to a variable
$web = Get-SPWeb $webURL 

#Get the SPList object to retrieve the List
$list = $web.Lists[$listName] 

#Create a new item
$newItem = $list.Items.Add() 

#Add properties to this list item
$newItem["Title"] = "Title Text"
$newItem["Description"] = "Description Text" 

#Update the object so it gets saved to the list
$newItem.Update()

$web.Dispose()

Contact Me for any technical help related with SharePoint 2010.

Monday, March 18, 2013

Determine Site / Site Collection Size (SharePoint 2007 & 2010)


1.   Open SharePoint 2010 Management Shell / Windows Command Prompt with Runas Administrator

2.   Enter below command to access "stsadm.exe" and press enter
cd "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN"
(Replace 14 by 12 for SharePoint 2007)

3.   Type below command and press enter
STSADM.exe -o enumsites -url "<Url of the web application>"
4.   Command will give text output where you can see site/site collection details like:
a.   Url
b.   Ower
c.   Content Database
d.   Storage Used MB
e.   Storage Warning MB
5.   Modify the command with text file path to get output in text file
Example:
STSADM.exe -o enumsites -url "<Url of the web application>" > c:\SiteDetails.txt

Contact Me for any technical help related with SharePoint 2010.

Wednesday, March 6, 2013

Open SharePoint Designer 2010 with Different User

Follow the steps to open SharePoint Designer 2010 with Different User
 

1.   Open windows command prompt by hitting "cmd" in Run 
2.   Enter the below command,
For 64bit, 
runas /profile /user:<domain\username> "c:\program files\microsoft office\office14\spdesign.exe" 
For 32 bit, 
runas /profile /user:<domain\username> "C:\Program Files (x86)\Microsoft Office\Office14\spdesign.exe"

3.   Enter the password of the user when prompted 
4.   SharePoint Designer will get opened with Different User


Contact Me for any technical help related with SharePoint 2010.