How to save each line of text file as array through powershell

The Get-Content command returns each line from a text file as a separate string, so will give you an array (so long as you don’t use the -Raw parameter; which causes all lines to be combined to a single string). [string[]]$arrayFromFile = Get-Content -Path ‘C:\USER\Documents\Collections\collection.txt’ In his excellent answer, mklement0 gives a lot more detail … Read more

What is the difference between a cmdlet and a function?

To complement Bruce Payette’s helpful answer: Not all functions are created equal in PowerShell: An advanced function is the written-in-PowerShell analog of a (binary) cmdlet (which, as stated, is compiled from a .NET language); decorating a function’s param(…) block with the [CmdletBinding()] attribute or decorating at least one parameter with a [Parameter()] attribute thanks, Ansgar … Read more

How do I deal with Paths when writing a PowerShell Cmdlet?

This is a surprisingly complex area, but I have a ton of experience here. In short, there are some cmdlets that accept win32 paths straight from the System.IO APIs, and these typically use a -FilePath parameter. If you want to write a well behaved “powershelly” cmdlet, you need -Path and -LiteralPath, to accept pipeline input … Read more

How to properly use the -verbose and -debug parameters in a custom cmdlet

$PSBoundParameters isn’t what you’re looking for. The use of the [CmdletBinding()] attribute allows the usage of $PSCmdlet within your script, in addition to providing a Verbose flag. It is in fact this same Verbose that you’re supposed to use. Through [CmdletBinding()], you can access the bound parameters through $PSCmdlet.MyInvocation.BoundParameters. Here’s a function that uses CmdletBinding … Read more

What is [cmdletbinding()] and how does it work?

Generally speaking, CmdletBinding is what makes a function into an Advanced function. Putting it at the top of a script makes the script an “advanced” script. Functions and scripts are much the same, where the script file name is equivalent to the function name and the script content is equivalent to the scriptblock section of … Read more