Richard's profileRichard Siddaway's BlogPhotosBlogLists Tools Help

Richard Siddaway

Interests
IT Architecture PowerShell Active Directory SQL Server Hill Walking Good Beer Science Fiction

Richard Siddaway's Blog

Of PowerShell and Other Things
June 08

Suspended

I won't be posting here for a while - until the current issues are sorted out.
 
Please see http://msmvps.com/blogs/RichardSiddaway/Default.aspx for all your PowerShell posts.

Upgrade?

Live.com has been upgraded – biggest change seems to be to the SkyDrive and we now get the ability to use Web versions of Office. May or may not be useful.

Big downside is that the statistics on the blog have been removed. Can’t see who is accessing or number of hits.  BAD MOVE – not happy about that.

 Live Writer has stopped working as well - VERY VERY BAD MOVE - very unhappy about that

June 07

Ranged letters

While I was playing around with the range operator I thought of this

001
002
003
004
005
$start = [byte][char]"a"
$finish = [byte][char]"h"

$start..$finish | 
foreach {New-Item -Path c:\test -Name "file$([char]$_).txt" -ItemType File}

 

Define your start and finish letters. Convert each to a char and then a byte.  This gives us the numeric value of the letter (ascii code).

We can then use those values in the range operator and create the files as we have already seen.

June 06

Using Range operator to generate letters

The range operator is used to generate a sequential set of numbers

1..10 | foreach {$_}

if we try this with letters

PS> a..j | foreach {$_}
The term 'a..j' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:5
+ a..j <<<<  | foreach {$_}
    + CategoryInfo          : ObjectNotFound: (a..j:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

However we can use the range operator to generate letters like this

## upper case
65..90 | foreach {"$_ $([char]$_)" }

## lower case
97..122 | foreach {"$_ $([char]$_)" }

using the ACSII codes.  if we want to create filea.txt – filej.txt  we can simply do this

97..106 | foreach {New-Item -Path c:\test -Name "file$([char]$_).txt" -ItemType File}

Nice and simple one line of PowerShell.

Using the range operator

One of the events in this years scripting games involved creating a set of files.  Many of the entries did something like this

001
002
003
$files = @("file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt", 
"file6.txt", "file7.txt", "file8.txt", "file9.txt", "file10.txt")
foreach ($file in $files) {New-Item -Path c:\test -Name $file -ItemType File}

 

A list of files is created and a foreach is used to call New-Item.  A variant on this used a for loop

for ($i=0; $i –le 9; $i++){New-Item -Path c:\test -Name $files[$i] -ItemType File}

 

There is a much easier way

001
1..10 | foreach {New-Item -Path c:\test -Name "file$_.txt" -ItemType File}

 

Whenever you have to deal with lists of things that are numerically sequential – try to use the range operator.