Get Content Databases per Web Application one-liner

Another one in the series of one-liners. This one will show you all Web Applications, and all associated Content Databses for these Web Applications:

Get-SPWebApplication | %{Write-Output "`n- $($_.url)"; foreach($cd in $_.contentdatabases){Write-Output $cd.name}}

The output will look similar to this:

image

You can output this to a text file by running the following:

Get-SPWebApplication | %{Write-Output "`n- $($_.url)"; foreach($cd in $_.contentdatabases){Write-Output $cd.name}} >> D:\filename.txt

Get current superuser/superreader accounts PowerShell One-liner

This one-liner wil show you the currently configured superuser and superreader accounts for each of your Web Applications.Just paste this line of code in your SharePoint 2010 Management Shell:

Get-SPWebApplication | %{Write-Host “Web Application: ” $_.url “`nSuper user: ” $_.properties[“portalsuperuseraccount”] “`nSuper reader: ” $_.properties[“portalsuperreaderaccount”] “`n”}

This is the output for the above command in my test environment:

image

In my case, I have not properly configured the super user accounts for all my Web Applications.

Be aware that misconfigured super user accounts can lead to access problems. You may not be able to access your SharePoint Web Application!

Add/Edit list items using PowerShell in SharePoint 2010

During the last few months I have been asked a few times to help think of a way to edit list values for lists that contain a lot of data. I immediately thought of PowerShell. This is why I want to show you what you can do with PowerShell.

Used set up for this demo
I have created a test environment for this demonstration. I will be using the following:
http://portal.contoso.com (SPWeb URL)
– Demo List (Custom List). This list has 1 extra added column: Information (Multiple lines of text)

image

Basics
To work with a list, you will have to start off by retrieving the list in PowerShell. For demonstration purposes, I will try to make it as easy and clear as possible:

#Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

#Variables that we are going to use for list editing
$webURL = <a href="http://portal.contoso.com">http://portal.contoso.com</a>
$listName = "Demo List"

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

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

These are the basics. With this, we can start to add items to this list or modify existing items.

Add items to the Demo List
So we got the list in our $list variable, now what?
To add an item to this list, we will need to use the Items.Add() method.

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

#Add properties to this list item
$newItem["Title"] = "My first item!"
$newItem["Information"] = "This is a multiple lines of text field `nSo I can use multiple lines"

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

Here is the output:

image

The `n part in: $newItem[“Information”] = “This is a multiple lines of text field `nSo I can use multiple lines”, is a New line. This works if you use the “Plain text” type, but not when you use “Rich text” or “Enhanced rich text”.

That was pretty easy right?

Add attachments to items
In the comments section, Nino asked if it was possible to add attachments for each new item created. Here is the way to do this.

$filebytes = [System.IO.File]::ReadAllBytes("D:\dummyfile.txt")
$newItem.Attachments.Add("DummyFile Title",$filebytes)

Edit existing list items
Now let’s say you want to edit the Information for this “first item”. We can do this with PowerShell too. Of course, if you would only change 1 item, you should do it by using the GUI, but what happens if you have to do the same thing for 5000+ items?

#Get all items in this list and save them to a variable
$items = $list.items

#Go through all items
foreach($item in $items)
{
#If the "Title" column value equals "My first item!" do something with it
if($item["Title"] -eq "My first item!")
{
#Change the value of the "Title" column
$item["Title"] = "My first edited item!"

#Update the item
$item.Update()
}
}

After running this part, we get the following result:

image

Hooray! It worked.

Now imagine all the things you can do with PowerShell and huge lists!
You could add column information for every item, or have a .csv or xml file which contains the items that should be changed. The possibilities are endless!