Archive for Powershell

SharePoint: How to remove a Content Database in SharePoint 2010 using PowerShell

 

PS C:\> Get-SPContentDatabase -Identity "ContentDB" | Remove-SPContentDatabase   Confirm
Are you sure you want to perform this action?
Performing operation "Remove-SPContentDatabase" on Target "ContentDB".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):y   Confirm
Removing 'ContentDB' will permananetly delete the SQL database, permananetly
deleting all content stored within it. Use Dismount-SPContentDatabase if you do
not want to delete the SQL database.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):y
PS C:\>

Disable List Deletion

If you don’t want users to be able to delete a specific SharePoint 2010 list, you can disable list deletion using Windows PowerShell. The example below demonstrates how to disable deletion of the tasks list on a Web site:

PS > $spWeb = Get-SPWeb http://SP01.powershell.nu
PS > $spList = $spWeb.Lists[“Tasks”]
PS > $spList.AllowDeletion = $false
PS > $spList.Update()
PS > $spWeb.Dispose()

Sharepoint 2010 – Cannot delete service application from Central Admin

 

When trying to delete a service application in Sharepoint 2010 from the central admin, sometimes you will find it just stays on the ‘processing’ window for hours (if you wait that long).

If it does you can try & delete the service application using Powershell:

1.Remove-SPServiceApplication

If that command doesnt work you can force the deletion by using STSADM:

 

1.STSADM -o deleteconfigurationobject -id {ID}

The above STSADM command is not documented by Microsoft (or doesnt seem to be)

Show and hide columns in SharePoint list forms with PowerShell

 

One of the great things about PowerShell with SharePoint 2010 is the ability to configure settings that are not available through the browser interface by writing a simple script, rather than having to load up Visual Studio and develop a custom application to access the Object Model. This article covers how to set properties on columns in order to show or hide them from specific list forms.

The following table taken from MSDN shows the properties available to us:

ShowInDisplayForm
Gets or sets a Boolean value that specifies whether the field is displayed in the form for displaying list items.

ShowInEditForm
Gets or sets a Boolean value that specifies whether the field is displayed in the form that is used to edit list items.

ShowInListSettings
Gets or sets a Boolean value that specifies whether the field is displayed in the page for customizing list settings.

ShowInNewForm
Gets or sets a Boolean value that specifies whether the field is displayed in the form that is used to create list items.

ShowInVersionHistory
Gets or sets a Boolean value that specifies whether the field is displayed in the page for viewing list item versions.

ShowInViewForms
Gets or sets a Boolean value that specifies whether the field is displayed in pages that are used to view list data.

For this example, I am going to disable the ShowInEditForm properties for a column called “Test Column” that I have created directly in a document library (I’ll deal with site columns later). Here is a screenshot of the edit form when I upload a new document to the library or edit the properties of an existing document:

TestColumnThere

I then run the following PowerShell Script:

#Get the web, list and column objects
$web = Get-SPWeb http://portal
$list = $web.Lists[“Shared Documents”]
$column = $list.Fields[“Test Column”]

#Change the ShowInEditForm property and update objects
$column.ShowInEditForm = $false
$column.Update()
$list.Update()
$web.Dispose()

Now when I edit the properties of the document, the Test Column is no longer there:

TestColumnThere

However, because the ShowInDisplayForm property is still set to true, the column does appear when I view the properties of a document:

TestColumnThere

The script is different if you want to achieve the same thing with a site column. I used the following script to modify the ShowInEditForm property of a site column called “Test Site Column”, which can be added to document libraries and lists directly, or through a content type. Note that the new ShowInEditForm property setting will apply to all document libraries, lists and content types where the site column has been added:

#Get the web and site column objects
$web = Get-SPWeb http://portal
$column = $web.Fields[“Test Site Column”]

#Set the PushChangesToLists property for the changes to be applied
#to lists where the column has already been added
$column.PushChangesToLists = $true

#Change the ShowInEditForm property and update objects
$column.ShowInEditForm = $false
$column.Update()
$web.Update()
$web.Dispose()

Add an Input Box to Your SharePoint 2010 PowerShell Scripts

 

For all of you busy SharePointers out there, this may be a helpful addition to your PowerShell scripts. Adding an input box to a script allows it to be reusable instead of only for a certain web app, site collection, or site. Think of it as an interactive way of passing parameters into PowerShell commands. It’s simple and easy, here’s how:

Add to the top of your script:

[void][System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’)

Call the input box and store the results in a variable:

$var_name = [Microsoft.VisualBasic.Interaction]::InputBox(“Input Description”, “Window Title”)

Use the variable where needed in your PowerShell script:

Get-SPLogEvent| ?{$_.level –eq $var_name} | select Category, Message | more

Example: