Pipeline

    PowerShell Pipeline ( | ) :

    The pipeline is one of the most powerful features of PowerShell. It allows you to chain together multiple commands to perform complex operations and manipulate large amounts of data. Here's how the pipeline works in PowerShell:

    The pipeline symbol (|) is used to connect the output of one command to the input of another command. The output of the first command is sent to the pipeline, which is then passed as input to the second command.

    Get-ChildItem | Select-Object Name, Length
    

    In this example, Get-ChildItem returns a list of files and directories in the current directory, which is then passed to Select-Object. Select-Object then selects only the Name and Length properties of each file and directory, which are then displayed in the output.

    Get-ChildItem | Where-Object {$_.Name -like "*.txt"} | Select-Object Name, Length
    

    In this example, Get-ChildItem returns a list of all files and directories in the current directory, which is then passed to Where-Object. Where-Object filters the list to include only files with a .txt extension, which are then passed to Select-Object. Select-Object selects only the Name and Length properties of each file, which are then displayed in the output.

     

    The pipeline allows you to work with large amounts of data efficiently. Instead of processing all the data at once, the pipeline processes the data one piece at a time, which can help reduce memory usage and improve performance.

    Get-ChildItem -Path C:\ -Recurse | Where-Object {$_.Extension -eq ".log"} | Select-Object FullName, Length | Out-File -FilePath "C:\logs.txt"
    

    In this example, Get-ChildItem returns a list of all files and directories in the C:\ drive and its subdirectories. Where-Object filters the list to include only files with a .log extension, which are then passed to Select-Object. Select-Object selects only the FullName and Length properties of each file, which are then passed to Out-File. Out-File writes the output to a file named logs.txt in the C:\ directory.

    The pipeline is a powerful tool in PowerShell that enables you to chain together multiple commands and manipulate large amounts of data efficiently. By mastering the pipeline, you can streamline your PowerShell scripts and achieve complex tasks with ease.