none
ForEach works but % does not work

    Question

  • Following works:
    foreach ($ADAPTER in gwmi win32_networkadapterconfiguration) {"MAC ADDRESS = " + $ADAPTER.mACADDRESS }

    Following does NOT work
    % ($ADAPTER in gwmi win32_networkadapterconfiguration) {"MAC ADDRESS = " + $ADAPTER.mACADDRESS }

    Is there something about the use of "%" that I'm not observing?

    Using CTP2 v2, Vista Ultimate (32 bit version), stand-alone laptop.

    karl

    Monday, December 01, 2008 4:54 PM

Answers

  • Marco,
    Via the Vistax64's powershell forum,
    I received the following link:

    http://bsonposh.com/archives/327

    Very interesting, and in-depth, discussion of the differences between foreach and foreach-object. 

    The link also references a couple of other links which I've not pursued, as of yet, but will shortly.


    Thanks,
      Karl


    Monday, December 01, 2008 5:37 PM

All replies

  • These are two different things.
    % is actually an alias for foreach-object, but simply foreach can be used also.  This is a cmdlet meant to be part of a PowerShell pipeline.

    So you have the syntax:
    some_cmdlet|foreach{}
    or
    foreach($item in $lists){some_commands}

    The latter is a language construct.

    For help:
    -%,foreach,foreach-object: PS>help foreach-object
    -foreach construct: PS>help about_foreach

    So how PowerShell interprets "foreach" will depend on where it is found.  If in a pipeline, then PowerShell treats it as a cmdlet.
    Monday, December 01, 2008 5:08 PM
  • Marco,
    I reread the help on foreach.  There is a length discussion of both inside and outside of a pipeline.  I have found nada which would restrict the use of the alias "%" instead of the alias "foreach" for foreach-object.

    If I've missed something, please let me know.   Right now, we seem to have a "sometimes" alias which misbehaves only  in certain circumstances.

    I use % (and ?) quite frequently and normally there is no problem. 

    To me this appears to be a "feature" (otherwise known as bug) in PowerShell.

    karl

    Monday, December 01, 2008 5:20 PM
  • OK, a little demo of the foreach-object cmdlet (with aliases "foreach" and "%"):

    PS>get-process p*|foreach-object{$_.name}
    powershell
    PSIService
    pthosttr
    putty
    putty
    PS>get-process p*|foreach{$_.name}
    powershell
    PSIService
    pthosttr
    putty
    putty
    PS>get-process p*|%{$_.name}
    powershell
    PSIService
    pthosttr
    putty
    putty
    PS>

    Now the foreach language construct:
    PS>$procs=get-process p*
    PS>foreach($proc in $procs){$proc.name}
    powershell
    PSIService
    pthosttr
    putty
    putty
    PS>

    OK, I've *never* used this syntax, but apparently this is also possible:
    PS>$procs=get-process p*
    PS>foreach-object -in $procs -process {$_.name}

    In the latter case, you are still using the foreach cmdlet.


    Monday, December 01, 2008 5:30 PM
  • Marco,
    Via the Vistax64's powershell forum,
    I received the following link:

    http://bsonposh.com/archives/327

    Very interesting, and in-depth, discussion of the differences between foreach and foreach-object. 

    The link also references a couple of other links which I've not pursued, as of yet, but will shortly.


    Thanks,
      Karl


    Monday, December 01, 2008 5:37 PM