Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Wednesday, May 22, 2013

Powershell script to read CSV file and XML

Working in SharePoint means a lot opportunity to use powershell. There is a specific powershell function very useful.

Import-csv $csvFilePath

This function reads the csv file and the first line will be the header line to create properties for each row.

For eg, if there is a csv file as

Name, Site, Url
aa,site a, http://sitea
bb,site b, http://siteb

After call Import-csv will load the data as a collection,
$data = Import-csv $csvfile

You can enumerate each row or filter them.
$data | foreach { $_. Name }
$data | where {$_.Name -eq "aa"}

And another convenient way to read data as xml format is just,
[xml] $xmldoc = Get-Content "xmldata.xml'

Sometimes if there is an error message such as "Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument", it is usually because of the mal-formatted XML.