PowerShell Cookbook

Search

Categories

 

On this page

Archive

Blogroll

Disclaimer
I work for Microsoft.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 218
This Year: 18
This Month: 0
This Week: 0
Comments: 529

Sign In

 Thursday, September 07, 2006
Thursday, September 07, 2006 4:59:38 PM (Pacific Daylight Time, UTC-07:00) ( )

The PowerShell range operator allows you to generate lists of numbers out of more compact expressions:

PS >1..5
1
2
3
4
5

PS >6..4
6
5
4

Something that came up on the newsgroup was the desire for range operators on data types other than the numeric ones.  It’s a feature we wanted to add, but weren’t able to.  In the meantime, this script / function accomplishes the goal quite well:

PS >range Friday Monday DayOfWeek
Friday
Thursday
Wednesday
Tuesday
Monday

PS >range y v char
y
x
w
v

## Range.ps1
## Support ranges on variable types other than numeric
## 
## range Stop Inquire System.Management.Automation.ActionPreference
## range Friday Monday DayOfWeek
## range e g char

param([string] $first, [string] $second, [string] $type)

$rangeStart = [int] ($first -as $type)
$rangeEnd = [int] ($second -as $type)

$rangeStart..$rangeEnd  | % { $_ -as $type }
Friday, September 08, 2006 12:14:58 AM (Pacific Daylight Time, UTC-07:00)
Is there a way to override that ".." range operator in PowerShell?

I am wondering if it is possible to change the behavior(or override) range operator depending on the types used in types.ps1xml.

So suppose that, if I am to override ".."'s behavior for the type "System.Management.Automation.ActionPreference",
"Stop..Inquire" (without quotes) would generate
Stop,Continue,Inquire array.
Friday, September 08, 2006 4:41:36 PM (Pacific Daylight Time, UTC-07:00)
PowerShell doesn't currently support language extensions. The only option right now is to use the postfix notation (ie: add 5 6, range 1 10 int) that functions and scripts provide.
Name
E-mail
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike, strong, sub, super, u)  

Enter the code shown (prevents robots):