Monthly Archives: February 2016

Working with type accelerators in PowerShell

Type accelerators are a great way in PowerShell to access .Net classes without having to write their full names. In essence they are aliases for .Net classes. If you have worked with PowerShell then you probably have used a type accelerator without knowing.

To get the list of available type accelerators the following command can be executed:

1
[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get

Depending on your version of PowerShell this will generate an output similar to this:

2016-02-26 (2)

Using the Get-TypeAccelerator function we can also display the available Type Accelerators in an easier to view way:

TypeAcc

It is useful for quickly defining objects without the complete notation of the class, for example compare the following two examples:

1
2
[System.Management.Automation.PSObject]@{'Property'='PowerShell Object'}
[pscustomobject]@{'Property'='PowerShell Object'}

Two type accelerators I use a lot are the [adsi] and [adsisearcher] Type Accelerators, this allows me to access and manipulate Active Dirclectory objects without relying on additional PowerShell modules. Here are some examples of querying Active Directory:

1
2
([adsisearcher]'samaccountname=jaap').FindOne()
[adsi]'LDAP://CN=Jaap,OU=AdminUsers,DC=JaapBrasser,DC=Com'

This allows for very short lines of code and used appropriately this can improve the readability of your code. Another few examples of type accelerators are the following:

1
2
3
4
[ipaddress]'8.8.8.8'
[version]'1.0.2.123'
[regex]::Replace('Type','e','e Accelerator')
[void] (Get-Process)

For more information on the different type and how to use them to define objects, queries or to access data you can view its article on MSDN. For example by executing the following code:

1
Start-Process -FilePath "http://social.msdn.microsoft.com/Search/en-US?query=$([psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get.wmisearcher.ToString())"

There are a number of ways to use Type Accelerators in PowerShell, do you already use them and if so what are some of your favorite uses of them. Feel free to discuss them below in the comments section.

PowerShell Fundamentals session at Experts Live on Tour

Earlier this week I was asked to speak at a community event, Experts Live on Tour (Dutch). Experts Live on Tour is series a free community events that cover a number of technical topics where attendees are led through the content by experts from the industry.

During the event we had an enthusiastic group of around 30 IT professionals and I walked through the the fundamentals of PowerShell. It was an engaging crowd so there was a lot of interaction between me and the audience. During the day I walked the crowd through the following topics:

  • PowerShell Fundamentals
  • PowerShell variables and objects
  • PowerShell loops, conditions and flow control
  • PowerShell tips from the field

During the day I mentioned a number of learning resources to, some of the topics I mentioned are listed below:

I have posted the assignments, slides and supporting documentation to GitHub, to view the files click here:

Yesterday a review sheet has been sent out to all attendees, I would love to hear back from you what you liked and disliked about the session. If you did not attend this event but would like to in the future, please let me or the friendly people at Experts Live know. There have been some questions if we have any plans for hosting an event for advanced scripters as well, if you are interested in this make sure that you make this known!

For more information about the topics in this article please visit the links below:

Links in this article
How to learn PowerShell
PowerShell Fundamentals – Course materials – GitHub
Experts Live on Tour