On the heels of writing about the join-path cmdlet the other day, I thought I would demonstrate a real-world task that deals with the file path differently. I saw this in an article on 4SysOps from a few years back, where the author is not using join-path but instead he uses the ‑LiteralPath parameter to get the same thing done.
But what’s even cooler than that is he demonstrates piping file paths into a PS function, one of the essentials of writing the kinds of devops automation scripts I write on-the-daily. This is a very handy way to have use a function loaded into your interactive PS CLI session, and point paths to it. Great article, I hope you read it!
In today’s example we’re going take the PS function from the article (and, again, I highly recommend a read-through of it), and use it to count the lines of text within each file under a specific path.
Let’s have a look at the code from the article:
|
|
So the function has a foreach loop to get a line count ( $content.Count ) for each text file processed. The code is place into the Process part of the function, because this allows you to get repeated runs for every item it receives from the <a href=“https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-7.4"pipeline.
Let’s see this in action. Say I am within a folder somewhere that has 5 files in it, all text files but with varying file types. To pipe everything in that folder, all 5 tezt files, into the function – I need only type:
C:\Temp> Get-ChildItem | Get-Lines
And that would get me:
|
|
Let’s just focus on line counts for the “File*” text files:
|
|
Or maybe we can make use of -LiteralPath to look at one specific file:
|
|
Or just go all wildcard for only files ending in txt :
|
|
So I hope this clever demo of paths and pipelines inside a PowerShell function helps illustrate just how powerful your scripts and CLI sessions can be. You can be working with data in myriad ways, and automate things to save yourself time.