Skip to content

Instantly share code, notes, and snippets.

@michaeltlombardi
Last active October 6, 2015 00:57
Show Gist options
  • Save michaeltlombardi/0edc921ac0f68fe2313d to your computer and use it in GitHub Desktop.
Save michaeltlombardi/0edc921ac0f68fe2313d to your computer and use it in GitHub Desktop.
Example of a New-<Whatever> Powershell function with default views.
Function New-Identity {
<#
.Synopsis
Creates a new identity.
.Description
Creates a new identity with UID, Email, and UPN.
.Parameter UID
The new identity's UID.
.Parameter Email
The new identity's Email address.
.Parameter UPN
The new identity's UPN.
.Notes
The code in the region "Define Default Display Properties" was discovered at the addresses below and all credit belongs to the authors:
* [Boe Prox](http://learn-powershell.net/2013/08/03/quick-hits-set-the-default-property-display-in-powershell-on-custom-objects/)
* [Rakhesh Sasidharan](http://rakhesh.com/powershell/naming-powershell-custom-objects-and-setting-their-default-display-properties/)
#>
param(
[Parameter(Mandatory=$true,
HelpMessage="Enter the UID of the identity you're trying to create.")]
[int32]$UID,
[ValidatePattern("^\S+@\S+\.\S+")]
[Parameter(Mandatory=$true,
HelpMessage="Enter the valid Email of the identity you're trying to create.")]
[string]$Email,
[Parameter(Mandatory=$true,
HelpMessage="Enter the UPN of the identity you're trying to create.")]
[string]$UPN
)
#region Define Default Display Properties
# Create the list of property names that will be in the default display set.
$DefaultProperties = "UPN","Email","UID"
# Use the list of property names to create the Default Property Display Set.
$DefaultDisplay = New-Object System.Management.Automation.PSPropertySet("DefaultDisplayPropertySet",[string[]]$DefaultProperties)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplay)
#endregion Define Default Display Properties
# Create the hash of the specified values for the Identity object.
$Properties = @{
"UID" = $UID;
"UPN" = $UPN;
"Email" = $Email
}
# Create the Identity with the parameters passed when the function is called.
$Identity = New-Object -TypeName PSCustomObject -Property $Properties
# Add the default display set to the identity.
Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $PSStandardMembers -InputObject $Identity
# Return the new identity to the pipeline.
Return $Identity
}
@michaeltlombardi
Copy link
Author

It's in the Notes section of the help, but it bears repeating:

The code in the region "Define Default Display Properties" was discovered at the addresses below and all credit belongs to the authors, Boe Prox and Rakhesh Sasidharan. As with most things, I didn't get here on my own.

Note: This is not the optimal solution. In a real system, you'd want much better data validation. This script is just as an example of a function with minor validation and a configured default property display set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment