Skip to main content

Posts

Showing posts from May, 2016

Saving data While it Travels Through the PowerShell Pipeline

A good question came up in class today while we were studying PowerShell Format commands.  We took a good look at how Format cmdlets consume the original object and make piping limited.  One of the questions that came up was if it is possible to save the information to a file and still be able to use it in the pipeline?  Fortunately for us, there is Tee-Object . We have a couple of ways that we can implement Tee-Object.  Let’s look at it from a reusability perspective.  Here is our starting code: Get-Process |     Select-Object -first 3 -Property Name , VM , PM And here is the output: Name            VM        PM ----            --        -- AcroRd32 472272896 216985600 AcroRd32 132603904  10866688 ApMsgFwd  96550912   1626112 We will now add Tee-Object and send the data to another cmdlet. Get-Process |     Select-Object -first 3 -Property Name , VM , PM |     Tee-Object -FilePath C:\PS\Data1.txt |     Sort-Object -Property VM T

Line Continuation in PowerShell – The Big Debate

A common point of confusion for IT Pros learning PowerShell is “when can I press the Enter Key?”  Line continuation allows us to prevent our code for moving horizontally off the screen.  When someone is reading your code and they must slide the horizontal scroll bar, well that is just annoying.  It makes your code less readable.  Let’s look at various opportunities that you have to write less annoying code. Let me start out with rule #1 when it comes to line continuation: Get your code to work first!!! What this means is that you write it on one line before trying to break it up.  If you start experimenting with how to break the line up without evening knowing if it works, you will be generating a lot of very difficult to resolve errors. Get your code to work first. The Backtick Let’s tackle the big one, the backtick.  It is also known as the grave accent. Depending on who you ask, they either love it or hate it. I personally love it, but only when you use it consi