Welcome to MSDN Blogs Sign in | Join | Help

Power and Pith

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

Published Saturday, October 21, 2006 5:13 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Power and Pith

I like!

Sunday, October 22, 2006 10:41 AM by Borat

# re: Power and Pith

I like it! Thank you!

Monday, October 23, 2006 11:26 AM by Julian Kay

# re: Power and Pith

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

Monday, October 23, 2006 2:43 PM by Stefan

# re: Power and Pith

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)

Wednesday, October 25, 2006 9:55 AM by jachymko

# re: Power and Pith

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 <<<<

Thursday, October 26, 2006 2:43 PM by Andrew

# re: Power and Pith

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

Friday, October 27, 2006 4:38 AM by David

# re: Power and Pith

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.

Friday, October 27, 2006 3:56 PM by Marcel

# re: Power and Pith

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!

Friday, October 27, 2006 6:33 PM by Jamie

# re: Power and Pith

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.

Saturday, October 28, 2006 10:49 AM by John

# re: Power and Pith

! 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

Saturday, October 28, 2006 12:06 PM by PowerShellTeam

# re: Power and Pith

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

Saturday, October 28, 2006 12:23 PM by John

# re: Power and Pith

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?

Friday, December 29, 2006 3:17 PM by MikeL

# re: Power and Pith

> 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

Friday, December 29, 2006 5:19 PM by PowerShellTeam

# re: Power and Pith

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.

Monday, March 12, 2007 11:38 AM by StitchJones

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker