Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Brian Bondy
Hub FS
Full List

Test-ItemProperty utility function

I was playing around in the registry the other day and found the PowerShell API lacking in a key area.  There does not appear to be a good way to detect the presence of a Registry Name/Value pair.  All of the operations such as New, Delete, Rename are based off of the program knowing the presence or absence of the key before hand.  This lacks symmetry with the rest of the APIs which have a test style function. 

PS> test-path "some\file\path\data.txt"
True

I took a few minutes and sketched out a basic Test-ItempProperty function.  It utilizes the Get-ItemProperty function and suppresses the rather loud red font error message via the –ErrorAction parameter (standard on all CmdLets). 

function Test-ItemProperty() {
    param ( [string]$path = $(throw "Need a path"),
            [string]$name = $(throw "Need a name") )

    $temp = $null
    $temp = Get-ItemProperty -path $path -name $name -errorAction SilentlyContinue
    return $temp -ne $null
}

Now I can finish up my happy scripting for the night

PS> test-path . IsItScriptingTime
True

Published Friday, June 12, 2009 10:00 AM by Jared Parsons

Filed under:

Comments

No Comments

New Comments to this post are disabled
Page view tracker