What does a PowerShell function written by Microsoft look like?

This question lingered in my mind for a long, long time.
Becaues my mean method of learning to script is: read script, understand it, know it…
I see code, patterns in there, loops, methods, understand what happens and I’ve gained new knowledge.

So naturally I wanted to see how Microsoft does it. In my eyes they can write scripts a lot better than I can.
But… cmdlets are cmdlets, not functions. If you want to see inside the cmdlet you’ll need something like Vistual Studio or some other tool.
I wanted to look inside it right from inside the PowerShell console!

As it seems, you can do this with functions… so let me take you step by step in doing this.

First: Take a look at the PSDrives by executing Get-PSDrive:

As you can see there is a PSDrive named ‘Function’. We’ll get back to that in a few lines…

Now let’s go to the second step: A function is part of a module. Import that module.

Now let’s get the content of that PSDrive named ‘Function’ by using the Get-ChildItem cmdlet:

Now we get to the  cool part. A function is basically plain text. So you can get plain text by using… Get-Content 🙂

So, here’s the code of the PowerShell function ‘Add-SCOMRunAsAccount’:

5 comments

  1. sstranger says:

    If you want to look within a Cmdlet install ILSpy and run the following:
    Ilspy (Get-Command Get-ScomAgent).dll

    Have fun!

    You can use Chocolatey to install ILSpy the easy way. http://chocolatey.org/packages?q=ilspy

    /Stefan

  2. Jeff Wouters says:

    Hi Stefan,
    Thanks 😀
    Jeff.

  3. Stijn Callebaut says:

    Just one question dough, why would one define a variable as ${variablename}?
    I do understand the ‘magic’ of ${C:TempTest.txt} = “Some Content” to store the string into a file using variable syntax. or ${=>} to use reserved syntax as a variable, but in the case of ${binary} or ${windows} this seems not to make any sense to me… Or am I missing something here?

  4. Jeff Wouters says:

    Hi Stijn,
    Good question. As far as I can gather it’s simply a different syntax.
    I will ask around and will get back to you on that one 🙂
    Jeff.

Leave a Reply

Your email address will not be published. Required fields are marked *