Tag Archives: powershell

PowerShell count number of characters in a string

I was just trying to count the number of characters in a string using PowerShell. It seems there is a very simple way to do it. It is a bit different if you are (me include) coming from a C language background like javascript, php or some other similar ones …

So the syntax is as follows:

1
2
3
4
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$measureObject = $string | Measure-Object -Character;
$count = $measureObject.Character;
Write-Host $count;

I have written the code above on the examples from Scripting Guy! Blog.

Get SharePoint list items and export them to XML using PowerShell

Last time I was explaining how to get sharepoint 2010 list items using powershell script. Now, lets make a step forward and export those items into an XML file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$outputXmlFilePath="C:\Exports\yourfile.xml";
$webURL = "http://sharepointsite";
$listName = "listUwant2export";

$spWeb = Get-SPWeb $webURL;
$spList = $spWeb.Lists[$listName];
$spItems = $spList.GetItems();

[System.Xml.XmlTextWriter]$xml = New-Object 'System.Xml.XmlTextWriter' $outputXmlFilePath, ([Text.Encoding]::UTF8);
$xml.Formatting = "indented";
$xml.Indentation = 4;

$xml.WriteStartDocument();
$xml.WriteStartElement('root');

$spItems | ForEach-Object {

$xml.WriteStartElement('item');

$xml.WriteAttributeString("ID",$_['ID']);
$xml.WriteAttributeString("Title",$_['Title']);

$xml.WriteEndElement();
Write-Host $_['Title'];
}

$xml.WriteEndElement();

$xml.Flush();
$xml.Close();
$spWeb.Dispose();

The result will be an XML output with values as attributes.

Get items from SharePoint List using PowerShell script

For long time I wanted to dive a bit deeper into PowerShell. Hopefully I will be able to post few of the sharepoint powershell tricks here.
First one is about getting list items from a sharepoint list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebURL = "http://sharepointsite"
$sourceListName = "mylist"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
#$spSourceItems = $spSourceList.GetItems()
#$spSourceItems = $spSourceList.GetItemById("1")
$spSourceItems = $spSourceList.Items | where {$_['ID'] -eq 1}

$spSourceItems | ForEach-Object {
    Write-Host $_['ID']
    Write-Host $_['Title']
}

You can either get all the items with GetItems() method currently commented out or you can filter items by some parameters. Filter applied in the script above is also equal to the GetItemById(“1”) method.

How to Create a Host Header Site Collection in WSS3 and SharePoint Foundation 2010

For WSS3 and MOSS you will have to open the CMD and use “stsadm” function. Here is the syntax:

1
stsadm -o createsite -url http://yourdomain.com -owneremail user@yourdomain.com -hostheaderwebapplicationurl htt://yourwebapplication.com

You can also give a check at the Microsoft Technet reference: http://technet.microsoft.com/en-us/library/cc262594%28office.12%29.aspx

In SharePoint Foundation 2010 you will have to use PowerShell which comes with Windows Server 2008.

First run:

1
Add-PSSnapin "Microsoft.SharePoint.PowerShell"

Now lets add a new site collection with host header (be sure to configure the command accodring to your server settings – link):

1
New-SPSite http://www.newdomain.com -OwnerAlias SERVER2008\Administrator -HostHeaderWebApplication http://server2008

Sooner or later you will also like to know which Site Collections there are in you Web Application (link). You can either look for it in Central Administration but you can also run a command in PowerShell:

1
Get-SPWebApplication http://server2008 | Get-SPSiteAdministration -Limit ALL | Select URL

Finaly, you do not want to use your Site Collection any more. Lets delete it (link):

1
RemoveSPSite -Identity "http://www.deletethis.com" -GradualDelete