I’ve been playing more with PowerShell lately, and there’s some really cool stuff in there! I though I’d post a few of my favorite discoveries. Generally speaking these are PowerShell user tips and tricks, because I’ve already been using PowerShell for a while, but since this is a “what I learned today” post, I’m not going to go back and write about the basics (besides, lots of other people are doing that already).

When you’re writing a PowerShell function, there is a method param which you can use to specify the parameters that the function accepts, including specifying their type, and their default value(s).

When you’re specifying types, you do so in square brackets like [System.Int32] but there are Type Shortcuts defined for the most common types, and even better, if you’re typing in PowerShell, you get tab-completion for them (even for the .Net types).

PowerShell includes a special type: [switch] which is for functions and scripts to take a command line parameter that is false by default (ie: it defaults to $false) and which doesn’t require any value on the command line (if you specify it, then it’s true). So for instance, if you had a script named “RandomLine” that looked like this:


param([switch]$Quiet, [string]$fileName='C:\Users\Joel\Documents\Quotes\attributed quotes.txt')

$line = Get-Content ( $fileName ) | Select-Random
Set-Clipboard -Text $line
if(!$Quiet) { "Clipboard set to:`n$line" }
 

You could call it by just writing RandomLine or you could suppress the message at the end (a plain string in PowerShell is equivalent to C# Console.WriteLine or a bash echo) by just specifying RandomLine -Quiet without needing to specify the value as $false.

Parameter names are not case sensitive, and don’t have to be fully typed out (as long as you type enough that the system can tell which one you mean), so you can actually call that script earlier as: RandomLine -q or RandomLine -q:$true -f:"C:\Users\Joel\Documents\Quotes\children's wisdom.txt"

Oh, and that script … depends on this other script:



param([array]$Collection)
 
begin {
    $result = $Seed
   
    if ($args -eq '-?') {
        ''
        'Usage: Select-Random [[-Collection] <array>]'
        ''
        'Parameters:'
        '    -Collection : The collection from which to select a random entry.'
        '    -?          : Display this usage information'
        ''
        'Examples:'
        '    PS> $arr = 1..5; Select-Random $arr'
        '    PS> 1..5 | Select-Random'
        ''
        exit
    }
 
    $coll = @()
    if ($collection.count -gt 0) {
        $coll += $collection
    }
}
 
process {
    if ($_) {
        $coll += $_;
    }
}
 
end {
    if ($coll) {
        $randomIndex = Get-Random -Min 0 -Max ($coll.Count)
        $coll[$randomIndex]
    }
}
 

Which actually depends on Get-Random, which is one of the PowerShell Community Extensions available on CodePlex.

Technorati Tags: , , , ,

Comments are closed.

Search
Similar Posts
    None Found
Recent Posts
    None Found