Huddled Masses
You can do more than breathe for free...
Browse: Home / What I learned today [17-Apr-2007] about [PowerShell]

What I learned today [17-Apr-2007] about [PowerShell]

By Joel 'Jaykul' Bennett on 17-Apr-2007

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: PowerShell, Scripting, Windows, Automation, Command Line

Similar Posts:

    None Found

| Tagged .Net, Development, PowerShell, Scripting

« Previous Next »

Lijit Search

Tags

.Net .Net 2008 Scripting Games Automation Bugs Design Development Funny Gadgets GeoShell GUI Huddled Masses Internet licensing Microsoft Modules My Software News Personal PInvoke Pipeline Politics PoshCode PoshConsole PowerBoots PowerShell PowerShell Functions PowerTips Rants Recommender Repository Scripting ShowUI Software Solutions Textile Tips User Group UserInterface WalkThrough WebHosting Windows 7 WordPress WPF Xml

About Huddled Masses

This is web site is dedicated to the musings of Joel Bennett (aka Jaykul) about technology, software, software development, the web, and the world.

Any resemblance of the views expressed and the views of my employer, my terminal, or the view out my window are purely coincidental. The resemblance between them and my own views is non-deterministic. The question of the existence of views in the absence of anyone to hold them is left as an exercise for the reader.

P.S.: I occasionally link to things I think are great. When I do, I occasionally find a "referral code" so I can make a little cash. I promise that I don't link to anything just because of that cash (I wouldn't cross the street for the amount of cash those links bring in, never mind write a whole blog post) ... but I do not promise that things I link to will stay great as time passes, nor that you will agree with me about their greatness!

Archives

  • April 2012
  • February 2012
  • January 2012
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • March 2011
  • February 2011
  • January 2011

Copyright © 2012 Joel Bennett.

Powered by WordPress and Hybrid.