SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Powershell Quick Parameters for Scripts

I was working on a script recently to manage a particular process and wanted to make it generic by allowing the user to pass in a parameter. I have seen lots of examples, especially those that work with SQL Servers, using text files and other items as parameters, but in this case I wanted an easy, quick, command like parameter.

This post looks at what I chose to check parameters. I had a couple requirements.

  • display message if no parameter is passed in.
  • display some help if /? is passed in.

I know that my cmdlets should contain help from the PoSh command line, and I’ll get to that. For now, I’m managing things the way I was taught when I wrote C. A /? should get me help.


I did a little research on parameters and found a few things, but decided to use the $args variable. This is an array of undeclared parameters. I grab the first value (the only one I care about like this.

$instance = $args[0]

Note the [0]. As with many things in Computer Science, we’re zero based arrays.

I could allow for other parameters, but this gets me what I want.


The test for /? is easy. That’s like this:

if ($instance -eq "/?") {
  write-host "Please enter the instance you wish to detach all databases from as a parameter."

If this is equal to my help request, write something out.

Next I needed to add another test. In this case I found that I could easily look for NULL variables, or blanks, with the !. As in this:

if (!$instance -or $instance -eq "/?") {
  write-host "Please enter the instance you wish to detach all databases from."

That worked well and lets me remind myself if I’ve forgotten to pass in a parameter. The one thing I experimented a few times with was the OR clause. I tried these, none of which worked:

  • if (!$instance OR $instance -eq "/?") {
  • if (!$instance) or ($instance -eq "/?") {
    if (!$instance) -or ($instance -eq "/?") {

A little experimenting got me to remember that PoSh is fairly consistent, and the plain -or should work inside the parenthesis.

Everything Else

When I first ran this without a parameter, my script froze. That’s because I hit the IF clause, wrote out the message, and then executed.

Fortunately I’ve done this type of stupid programming before, so I added this:

if (!$instance -or $instance -eq "/?") {
  write-host "Please enter the instance you wish to detach all databases from."
else {

The rest of my script fits in the else clause.


Filed under: Blog Tagged: powershell, syndicated

The Voice of the DBA

Steve Jones is the editor of SQLServerCentral.com and visits a wide variety of data related topics in his daily editorial. Steve has spent years working as a DBA and general purpose Windows administrator, primarily working with SQL Server since it was ported from Sybase in 1990. You can follow Steve on Twitter at twitter.com/way0utwest


Leave a comment on the original post [voiceofthedba.wordpress.com, opens in a new window]

Loading comments...