Good Command Line Arguments to Powershell Scripts

I do a fair bit of Powershell scripting, and one thing that’s always frustrated me is the ability to get good command line argument parsing. What I really wanted was something better than bare argv parsing- something that would pair arguments and values. I’ve googled for this several times, but never came up with anything. It turns out to be pretty easy. A trivial example follows:

pre(code).Param($Date = ’2011-6-13’, $Blocked=$false, $days=14)
Write-Host $Date $Blocked $Days

Then you invoke this like so:

.\test.ps1 -Date 2011-1-1 -Blocked true -days 1

You can always leave off any argument since you’ve defaulted the values in the Param declaration.

One thing I haven’t found is a way to invoke a function in Param, for example, defaulting $Date to DateTime.Now

— Gordon Weakliem

---

Comment

Commenting is closed for this article.