Using a list or array as a PowerShell script input parameter

One of my colleagues created a PowerShell script that we use to migrate SharePoint 2010 sites from the SharePoint 2007 interface (UI 3) to the SharePoint 2010 interface (UI 4). The script works rather well for updating one or two sites at a time:

Set-UserInterface2010.ps1 -url "https://my.sharepoint.site/path/"

Today I received a request to update a list of 32 sites. After updating one, I thought–this is going to be tedious. We can improve this.

First, I updated the parameter to accept an array of strings instead of a single string.

Before
param([string]$url = $(throw "Please specify a Site URL to convert to the SP2010 look and feel"))

After
param([string[]]$url = $(throw "Please specify a Site URL to convert to the SP2010 look and feel"))

Next, I wrapped the call to the main function in a ForEach loop:
ForEach ( $siteurl in $url ) {
...
# Throw in a line break to separate output
Write-Host ("`n`n")
}

The script can still take a single site as input (the string is treated as a string array with a single element), but now I can also pass it a list:

Set-UserInterface2010.ps1 -url "https://my.sharepoint.site/path1/","https://my.sharepoint.site/path2/","https://my.sharepoint.site/path3/"

Here’s hoping that taking a couple minutes to update the script and running it once saved me more time than running it 32 times!

4 thoughts on “Using a list or array as a PowerShell script input parameter”

  1. Hi, I have a java code that execute a power shell script to do something on dns server, the parameters are in a string array and I want to put them in that script in my java code.
    would you please help me?

  2. Hi

    You could also do:
    url1, url2, url3 | foreach-object {Set-UserInterface2010.ps1 -url $_}
    but next time you should write the pipe and foreach-object again.
    If you can change the code this seems me a good way to change the code.

    Kind regards

  3. Hello Chris,

    this was exactly was I was looking for. Thanks a lot! 🙂

    Kind regards

Leave a Reply

Your email address will not be published. Required fields are marked *