The pipeline - using output from a PowerShell cmdlet

suggest change

One of the first questions people have when they begin to use PowerShell for scripting is how to manipulate the output from a cmdlet to perform another action.

The pipeline symbol | sends a command's output objects to the next command. A simple example uses Select-Object to keep only the Name property of each item from Get-ChildItem:

Get-ChildItem | Select-Object Name
#This may be shortened to:
gci | Select Name

More advanced usage of the pipeline allows us to pipe the output of a cmdlet into a foreach loop:

Get-ChildItem | ForEach-Object {
    Copy-Item -Path $_.FullName -destination C:\NewDirectory\ 
}

#This may be shortened to:
gci | % { Copy $_.FullName C:\NewDirectory\ }

The example uses the $_ automatic variable, which is the current pipeline object. In Windows PowerShell 3.0 and later, $PSItem is the same variable. $_ is not an alias created with Set-Alias.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents