Adding a Yes, No, Cancel prompt to a PowerShell script

When I write PowerShell scripts I tend to want to step through sections, this allows me to decided whether to proceed with sections or not. I achieve this by using the ChoiceDescription class as demonstrated below.

[code lang=”powershell”]
## The following four lines only need to be declared once in your script.
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $cancel)

## Use the following each time your want to prompt the use
$title = "Title" $message = "Question?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
0{
Write-Host "Yes"
}1{
Write-Host "No"
}2{
Write-Host "Cancel"
}
}
[/code]

More information about using prompts in PowerShell can be found in the following article and tip on Microsoft TechNet.

Clear the SharePoint Quick Launch using PowerShell

Today I had a requirement to remove all the headings and links from the quick launch navigation of hundreds of SharePoint sites. The sites were being provisioned as part of a PowerShell deployment script that was deleting the default list and libraries. Going through each of these sites manually was not an option – so I edited the deployment script to include a function to remove the headings for me.

SharePoint Quick Launch with Headings
SharePoint Quick Launch with Headings

I remembered doing something similar to this back on SharePoint 2007 but I didn’t have access to the previous script or project and instead had to research the subject for a while to find what I needed.

Solution

A post from Get-SPScripts supplied me with what I was after, although it was part of a much larger script. So I picked away at their code and made it into the following PowerShell function to re-use in other projects.

[code lang=”powershell”]
function Remove-SPQuickLaunchLinks($url){
$SPWeb = Get-SPWeb $url
$QuickLaunch = $SPWeb.Navigation.QuickLaunch
$Nodes = @()

$QuickLaunch | ForEach-Object {
$Nodes = $Nodes + $_.Id
}

$Nodes | ForEach-Object {
$Node = $SPWeb.Navigation.GetNodeById($_)
$Node.Delete()
}
$SPWeb.Dispose()
}

Remove-SPQuickLaunchLinks "http://sharepoint.jcallaghan.com/"
[/code]

The above Remove-SPQuickLaunchLinks function will remove all headings and links from the SharePoint quick launch for a particular site.

Empty SharePoint Quick Launch
Empty SharePoint Quick Launch

Download

You can download the script here. Remember to review, rename and test this script before using it in a production environment.

Review, rename and test

Review

It goes without saying…but whenever you download and use any code or script from this site, or any site for that matter, you should review and understand what the code or script is doing before you attempt to use it.

Rename

Using the PowerShell scripts that I share as an example. I distribute these scripts as text files as it provides an opportunity for the script to reviewed, after which you can knowingly rename the file. This also prevents any hastily or accidental execution of the script.

Test

And as with anything you download from the internet – you should test it in a dedicated environment prior to any production environment.