Invocation Operators, States and Scopes

Published: 09-30-2017

I’m going to start off with invocation operators, but don’t worry it’ll come around full circle. If you’re here just for scopes, you can skip to the PSModuleInfo section.

Common Knowledge

In general you can think of the invocation operators & and . as a way of telling PowerShell “run whatever is after this symbol”. There are a few different ways you can use theses symbols.

Name or Path

& .\Path\To\Command.ps1

Invoke whatever is at this path. The path can be a script, an executable, or anything else that resolves as a command.

& Get-ChildItem

Invoke a command by name. You don’t typically need an operator to invoke a command, but what you might not know is you can also use the . operator here to invoke a command without creating a new scope.

Read more…

Formatting Objects without XML

Published: 04-20-2017

Custom formatting in PowerShell has always seemed like one of the most under utilized features of PowerShell to me. And I understand why. It feels kind of bizarre to spend all this time writing PowerShell, creating a cool custom object and then jumping into…XML.

The bad news is, what I’m about to tell you is still going to involve XML. The good news, you don’t have to write it.

PSControl Classes

One of the things I love the most about PowerShell is the pure discoverability it provides. You can usually figure out what you need to do with just about any command, object, etc. That all gets a little abstracted when you’re working through XML.

The PSControl object (and more importantly it’s child classes) give that discoverability back. Let’s pipe TableControl to Get-Member so you can see what I mean.

PS C:\> [System.Management.Automation.TableControl] | Get-Member -Static

   TypeName: System.Management.Automation.TableControl

Name            MemberType Definition
----            ---------- ----------
Create          Method     static System.Management.Automation.Table...
Equals          Method     static bool Equals(System.Object objA, Sy...
new             Method     System.Management.Automation.TableControl...
ReferenceEquals Method     static bool ReferenceEquals(System.Object...

Let’s take a closer look at the Create static method.

Read more…

Cmdlet Creation with PowerShell

Published: 04-13-2017

I was looking through the PowerShell-Tests repo and I saw they were creating Cmdlets inline for some tests. They were using C# and Add-Type which got me wondering.

Can you create one using pure PowerShell? Yeah, you can. With a little extra handling.

Building the Cmdlet

using namespace System.Management.Automation
using namespace System.Reflection

# Basic do nothing Cmdlet.
[Cmdlet([VerbsDiagnostic]::Test, 'Cmdlet')]
class TestCmdletCommand : PSCmdlet {
    [Parameter(ValueFromPipeline)]
    [object]
    $InputObject;

    [void] ProcessRecord () {
        $this.WriteObject($this.InputObject)
    }
}

Just a basic cmdlet that passes anything it gets from the pipeline. But right now, it’s just a class. Now we need to tell PowerShell it’s a cmdlet. This is where we have to stretch a little.

Read more…