Monday, March 29, 2010

SharePoint 2010 and Powershell

Scenario:
Powershell is making its way in all the Microsoft Server products and also is a great scripting language for SP2010. In SP2007 we had STSADM ( which is still not dead fully ). I was just playing around (again) on a small script I want to test.

Solution:
Please login to SharePoint 2010 box and Start Powershell. This will load the SharePoint snap-in automatically. Which mean Powershell is now ready and under stand SharePoint APIs.

I have used some commandlets from the following Powershell Blog( awesome )

Powershell Snippets :

# Declaring a variable 
$sitecollectionurl = "http://sp2010/sites/client3"

# Concating string
$weburl = "$sitecollectionurl/subsite"

# Logging current date
"$(Get-Date -f o) Start Logging"

# Changing the current directory
cd 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\'

# Calling the STSADM commands
.\STSADM.EXE -o createsite -url "http://sp2010/sites/client1" -sitetemplate STS#1 -owneremail "admin@email.com" -ownerlogin sp2010\administrator -title "client1"

# Calling the STSADM commands using variable
.\STSADM.EXE -o createsite -url "$sitecollectionurl" -sitetemplate STS#1 -owneremail "admin@email.com" -ownerlogin sp2010\administrator -title "client1"

# Calling a Powershell CommandLets
.\Add-SPList -url "http://sp2010/sites/client1" -Name "ClientDocuments" -Description "Client Docs" -Type "Document Library"

# Calling a Powershell CommandLets using variable
.\Add-SPList -url "$sitecollectionurl" -Name "ClientDocuments" -Description "Client Docs" -Type "Document Library"

# Output to a new file ( single arrow )
"I am saving this message to a file" > "c:\output.txt"

# Append output to an existing file ( double arrow )
"I am saving this message to a file" >> "c:\output.txt"

0 comments: