Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does |% mean in PowerShell

Tags:

powershell

I've come across this today:

$tests = (Get-ChildItem . -Recurse -Name bin |% { Get-ChildItem $_ -Recurse -Filter *.Unit.Tests.dll } |% {  $($_.FullName) })
Write-Host ------------- TESTS -----------
Write-Host $tests
Write-Host -------------------------------
.\tools\xunit\xunit.console.exe $tests -xml test-report.unit.xml

I can't get my head around what '|%' is doing there. Can anyone explain what it used for please?

like image 897
miticoluis Avatar asked Oct 06 '16 14:10

miticoluis


People also ask

What does @{} mean in PowerShell?

The Splatting Operator To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol.

What does the & symbol mean in PowerShell?

& is the call operator which allows you to execute a command, a script, or a function.

What does a question mark mean in PowerShell?

In Powershell the question mark is an alias of Where-Object cmdlet: The '?' symbol and Where are both aliases for Where-Object. While $_ is the result/output from the pipeline as stated here. Contains the current object in the pipeline object.

What does double colon mean in PowerShell?

There are no separate math functions in PowerShell. So when you need to access a maths function such as Round or Sqrt, just call for the System. Math. If you have ever wondered about the PowerShell double colon (::), then all is revealed, it means a static method.


1 Answers

% is an alias for the ForEach-Object cmdlet.

An alias is just another name by which you can reference a cmdlet or function. For example dir, ls, and gci are all aliases for Get-ChildItem.

ForEach-Object executes a [ScriptBlock] for each item passed to it through the pipeline, so piping the results of one cmdlet into ForEach-Object lets you run some code against each individual item.

In your example, Get-ChildItem is finding all of the bins in a certain directory tree, and then for each one that is found, Get-ChildItem is being used to find all of the files underneath that match *.Unit.Tests.dll, and then for each one of those, the full name is returned to the pipeline (which is getting assigned to the $tests variable).

like image 196
briantist Avatar answered Sep 30 '22 21:09

briantist