Using Tee-Object to see intermediate results
Debugging long pipelines can be tricky. While it is possible to split a pipeline over several source lines in order to set a breakpoint on a particular segment of a pipeline, it is often the cumulative results of the pipeline that are important. In cases where the end result is not what is expected, it can be helpful to be able to see what the intermediate results are in the pipeline. For instance, consider the following (incorrect) code to find the largest items in a folder structure:
#find largest 5 items in the directory tree dir -recurse | sort-object Length | select-object -last 5
Since the output is incorrect, we can insert Tee-Object
commands into the pipeline to save the intermediate results (after dir
and after sort
) into variables or files for our convenience. First, let's look at how to get the results into files using the following code:
#find largest 5 items in the directory tree dir -recurse | tee-object -FilePath c:\temp\files...