ActiveDirectory: Create new Security Group (One Liner)

This is a rather long one liner but it pipes the Organisational Unit into the New-ADGroup cmdlet. If you look at the TechNet document for New-ADGroup, The -path variable (OU’s distinugished name) takes a pipe line but by property. In Microsofts words, this means:

— ByPropertyName: Parameters that accept input “by property name” can accept piped
objects only when a property of the object has the same name as the parameter.

Ref: http://technet.microsoft.com/en-us/library/hh847902.aspx

In other words, Get-ADOrganizationalUnit has to have an output of “Path” or New-ADGroup has to have an input of distinguishedName. In this case, neither is true, but we can use a custom expression to make it think it has an output of Path.

Call Get-ADOrganizationalUnit, passing a custom expression of “path”:

Which returns the distingushedName as “Path”. We can then pipe this into New-ADGroup:

And if you like to add some information to the Notes field, you can do that with:

So, putting it all together:

 

 

Please feel free to leave a comment...