none
what does "%" mean in powershell syntax

    Pergunta

  • I know that typically the "%" character is for mod functionality.  In some powershell scripts I've come across, I've noticed % being used after the | character and I can't seem to find what the syntax means.

    For example. "$old | % { Remove-Item $_.SPBackupDirectory -Recurse }"   I can't figure out what | % is doing.....  is it a shorthand foreach expression?

    Tony Testa www.tonytestasworld.com
    segunda-feira, 15 de fevereiro de 2010 13:42

Respostas

  • Yes, it is a shortcut notation or alias for ForEach-Object:

    PS > get-alias -definition foreach-object
    
    CommandType     Name                                                Definition
    -----------     ----                                                ----------
    Alias           %                                                   ForEach-Object
    Alias           foreach                                             ForEach-Object


    The above only works with PowerShell v2.  V1 doesn't support the -Definition parameter.
    segunda-feira, 15 de fevereiro de 2010 13:46

Todas as Respostas

  • Yes, it is a shortcut notation or alias for ForEach-Object:

    PS > get-alias -definition foreach-object
    
    CommandType     Name                                                Definition
    -----------     ----                                                ----------
    Alias           %                                                   ForEach-Object
    Alias           foreach                                             ForEach-Object


    The above only works with PowerShell v2.  V1 doesn't support the -Definition parameter.
    segunda-feira, 15 de fevereiro de 2010 13:46
  • Also this is math operator: division with a reminder. When percent sign is placed after pipeline sign "|" — it always threated as Foreach-Object alias. In all other cases it is threated as math operator.
    http://www.sysadmins.lv
    terça-feira, 16 de fevereiro de 2010 16:22
  • Not quite.  For example:

    % -process {$_.name} -inputobject (gci profile.ps1)
    

    sexta-feira, 28 de fevereiro de 2014 18:17