Frederic Mokren
Frederic Mokren

Reputation: 31

Use of % operator in pipleline

I ran across a Powershell script on the internet that uses the '%' operator in a way I hadn't seen before.

For the life of me I can find any documentation that describes what the '%' operator's purpose is in the script.

Here is a contrived example.

Get-ChildItem -Path $env:windir | % { Write-Host $_.FullName }

Can anyone point me to documentation on this use of the '%' operator?

Upvotes: 2

Views: 1545

Answers (3)

NextInLine
NextInLine

Reputation: 2204

As per this blog post, use Get-Alias ? to see all the single-character aliases.

The most common are

  • % - ForEach-Object
  • ? - Where-Object

If you're writing a script, it's better to use the full terms so that it is more readable for others (or even yourself in the future). PowerShell has a tab-complete feature; for instance, for<tab><tab> results in ForEach-Object.

Upvotes: 5

Hackerman
Hackerman

Reputation: 12305

Yes, it's an Alias of ForEach-Object. You can use the following snippet in order to check the alias:

Get-Alias | where-object {$_.Definition -eq 'Foreach-Object'} 

This produces the following output:

Alias           % -> ForEach-Object                                                                                                                                                           
Alias           foreach -> ForEach-Object

And that means that % is an alias for Foreach-Object(foreach is only an alias for Foreach-Object in the pipeline, as pointed out by @EBGreen)

The only documentation that I could fine about the subject is this one: https://ss64.com/ps/foreach-object.html

Upvotes: 1

Adam
Adam

Reputation: 4168

There percent operator is short hand for ForEach-Object (or ForEach). If you want documentation proof, try...

Get-Help %

Upvotes: 0

Related Questions