Write Verbose Output in PowerShell using Write-Verbose

It is good practice to use the various -Verbose, -Debug and other about_CommonParameters when writing functions because it helps one to write the clean code in a very integrated way with the PowerShell. This blog post focuses on the usage of Verbose command to provide detailed tracking information and status. One of the main benefits about using Write-Verbose command is that you can control, if you need extra detailed information. By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference variable or using the Verbose common parameter in any command. Also, Write-Verbose writes to the verbose output stream and you can capture it separately.
Continue reading “Write Verbose Output in PowerShell using Write-Verbose”

Return Multiple Values from PowerShell Function

In PowerShell functions, by default, you can return only one object at a time. Since PowerShell does not apply any restriction on data type returned, it created a lot of possibilities on what can be returned as an output of the function. So if one needs to return multiple values or objects, it is generally suggested to create an array of the objects and then return the array. If the underlying values are simple strings, some would create a custom PSObject and then return the PSObject. In this blog post, we will discuss the other methods to return the multiple values from PowerShell functions.
Continue reading “Return Multiple Values from PowerShell Function”

Implement Pipeline Support by making proper use of ValueFromPipeline and ValueFromPipelineByPropertyName in PowerShell functions

In our previous post, we learned how to make use of Begin, Process and End blocks to implement proper pipeline support. Building on to that, in this blog post, we’ll learn further about when to make use of ValueFromPipeline property and when to make use of ValueFromPipelineByPropertyName and what happens behind the scenes.

Multiple parameters that accept Pipeline Input

What if there are multiple parameters that can accept pipeline input and we need to run the Process block for them. Let’s consider below code: Continue reading “Implement Pipeline Support by making proper use of ValueFromPipeline and ValueFromPipelineByPropertyName in PowerShell functions”

Implement Pipeline Support by making proper use of begin, process and end blocks in PowerShell functions

Of many things that make PowerShell stand apart in the world of scripting languages, perhaps two are most fundamental to it: first, its treats everything as Objects and second, the ability to pipe objects from one cmdlet to another. Using this capability, we can effortlessly link multiple cmdlets together. Doing this will also throttle the amount of memory that is being allocated (in most cases) that the current session is using for the commands. So, its very natural that you would want to implement pipeline support for your own function, that you just wrote.
Continue reading “Implement Pipeline Support by making proper use of begin, process and end blocks in PowerShell functions”

Write Advanced functions in PowerShell using various Write Cmdlets

PowerShell has become de-facto tool of choice for automation in Microsoft world from long time and slowly it is winning over hearts of the Linux administrators as well. Just like with other programming languages, there are many ways to do the same thing in PowerShell. However they differ in little subtle ways. You may or may not notice them in your day to day usage, but if you learn those subtleties, you can quickly improve the performance and results of your automation. This blog post is about one of the such cases only.
Continue reading “Write Advanced functions in PowerShell using various Write Cmdlets”