Power and Pith

Power and Pith

  • Comments 15

In our newsgroup, Benny asked the question, “How to get subdir items with full path name?”

ClaudioG64 responded:

PS> get-childitem C:\ | foreach-object -process { $_.FullName }

Dreeschkind replied that we had pithier ways of doing it posting:

PS> gci C:\ | % { $_.FullName }

It was at this point that I realized that many of you don’t have a function that many of us in the team use.  So put your seatbelts on and try this one out:

PS> ${function:...} = { process { $_.($args[0]) } }
PS> gci c:\ | … FullName

With Windows PowerShell, you can be Powerful and Pithy!

Enjoy

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post
  • I like!

  • I like it! Thank you!

  • Why don't you use "gci c:\ | select-object fullname" ?

  • Stefan, you don't want to use select-object because it returns a collection of PSCustomObjects, each one with a FullName property.

    This function returns only the actual values of the property (eg. strings, ints, not wrapped in a PSCustomObject)

  • I tried your suggestion, i.e., ${function:...} and I get errors.  What is wrong?  This is RC2 of the powershell running in XPSP2.  See below...

    Windows PowerShell

    Copyright (C) 2006 Microsoft Corporation. All rights reserved.

    PS U:\> ${function:...} = { process { $_.($args[0]) } }

    PS U:\> gci c:\ | _ FullName

    The term '_' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

    At line:1 char:12

    + gci c:\ | _  <<<< FullName

    PS U:\> gci c:\ | _FullName

    The term '_FullName' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try

    again.

    At line:1 char:19

    + gci c:\ | _FullName <<<<

    PS U:\> gci c:\ | FullName

    The term 'FullName' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try

    again.

    At line:1 char:18

    + gci c:\ | FullName <<<<

  • Andrew: try using a real name for the function (replace "..." with something useful :)

  • I like to use the name pick, however, "..." works just fine.  The problem Andrew is having isn't because he's using "..." as the name.  The problem is he's creating a function called "..." but in his tests he's using "_" instead.

  • I had two questions about this function:

    1. What are the rules for non-alphanumeric function names?  I tried creating a function !! to give me the last command line (like csh), but I can't seem to overload the !.

    2. In the function body is the command 'process', which appears to be shorthand (not an alias) for get-process.  I see the same functionality for the noun 'item', but not for 'object'.  What are these and how do the work?

    Pretty cool stuff!

  • Jamie,

    !! seems to have some special meaning, though I'm not sure what it is.  $$ is the variable for getting the last command that was run, though.

    As for the process {} bit, this mimics the Process() method in a cmdlet.  What that means is that whatever's passed along the pipeline will get passed to the process part of the function.  begin {} and end {} are called before the first item is processed, and after the last item respectively.

  • ! is a synonym for the -not operator:

    PS (1) > ! $true

    False

    PS (2) > !! $true

    True

    PS (3) > -not  $true

    False

    PS (4) > -not -not  $true

    True

    You can define a function called !!

    PS (5) > function !! { "Hi" }

    but you have to use the call operator '&' to call it:

    PS (6) > & !!

    Hi

    -bruce

    =========================================================

    Bruce Payette

    Windows PowerShell Technical Lead

  • Hi Bruce,

    I thought it'd be something like that but a quick scan of about_operator didn't reveal it.  I just checked about_logical_operator, and it's mentioned there.  It's nice that the ! operator is kept from C#, though I note that && and || aren't kept (for rather obvious reasons, I'd guess).

    (Eagerly awaiting your book, by the way!  I'd have signed up for early access, but I already ordered it from Amazon.co.uk, and I like having the physical book)

    Thanks

    John

  • I just started with PowerShell.

    Wanted to run some test scripts from you download.

    When I tpye in Beep.ps1 I get "The term 'Beep.1' is not recognized....."

    What Am I doing wrong?

  • > When I tpye in Beep.ps1 I get "The term 'Beep.1' is not recognized....."

    > What Am I doing wrong?

    You are relying upon a traditional bad shell behaviour that has been a security nightmere for decades.

    In PowerShell, you have to be explicit if you want to run a command in the current directory.  Type ".\beep.ps1"

    Jeffrey Snover [MSFT]

    Windows PowerShell/MMC Architect

    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • Thank You for supplying the ".\*" information.  I have been racking my brain for almost two days wondering what I was doing wrong.  And to think it was as simple as using the PROPER .\yourscripthere.ps1 format.

    Thank you very very much.

  • Is there any chance we could get this built-in to the next version?

Page 1 of 1 (15 items)