Skip to main content

PowerShell 4.0 Wishlist - Select-Object Enhancement

I mostly like the way Select-Object allows you to create custom objects and properties. However, I really wish it was simpler to create properties with ScriptBlock values without the lengthy Hashtable definition.

If only Select-Object would implement something like this so you can define a property ScriptBlock without the long Hashtable definition. Similar to how this custom function I wrote allows you to define a property name as the Key of the Hashtable and the value as the ScriptBlock or name of the property.

Wishlists are usually wishful thinking, there are probably reasons why Select-Object behaves like it does, it isn't that bad since you can short-hand the keys but it just doesn't seem as fluid. Here is to wishing for a better Select-Object.
  1. function ConvertTo-PSCustomObject {  
  2.     [CmdletBinding()]  
  3.     param(  
  4.         [ValidateNotNullOrEmpty()]  
  5.         [Parameter(Mandatory = $true, Position = 0)]  
  6.         [Array] $Property,  
  8.         [Parameter(Mandatory = $true, ValueFromPipeline = $true)]  
  9.         $InputObject  
  10.     )  
  12.     begin {  
  13.         $Properties = $Property |% {  
  14.             if ($_ -is [Hashtable]) {  
  15.                 $_.GetEnumerator() |% {  
  16.                     if ($_.Value -is [ScriptBlock]) {  
  17.                         @{Name="$($_.Key)";Expression=$_.Value}  
  18.                     } else {  
  19.                         @{Name="$($_.Key)";Expression=[ScriptBlock]::Create("`$_.'$($_.Value)'")}  
  20.                     }  
  21.                 }  
  22.             } else {  
  23.                 $_  
  24.             }  
  25.         }  
  26.     }  
  28.     process {  
  29.         $InputObject | Select-Object -Property $Properties  
  30.     }  
  31. }  
  33. gci $ENV:TEMP -Directory | ConvertTo-PSCustomObject Extension,@{FileName='Name'},@{Age={(Get-Date) - $_.CreationTime}}  


  1. I have a similar request for PSCustomObject literals! I made a Gist to demonstrate how this would work by creating a new function called New-PSObject but it'd be awesome if the object literal syntax also worked.

    One difference with mine is it determines whether the scriptblock should be created as a ScriptMethod or ScriptProperty based on the presence of a param() block.


Post a Comment

Popular posts from this blog

PowerShell SupportsShouldProcess Worst & Best Practices

This has been a very big discussion within the Scripting Games 2013 community and I want to add my two cents in an official blog post.

I've left several people comments on how they might be misunderstanding how SupportsShouldProcess works, but I also realize, everyone of these individuals has given me more insight into its use and perhaps, how it should best be utilized.

For those of you that don't know, SupportsShouldProcess is a parameter on the CmdletBinding attribute you can place on your cmdlets that automatically adds the -WhatIf and -Confirm parameters. These will naturally flow into other cmdlets you use that also SupportsShouldProcess, e.g. New-Item, Move-Item.

The major discussion has been around, should you just let the other cmdlets handle the $PSCmdlet.ShouldProcess feature, and if not how should you implement it. ShouldProcess has the following definitions.

FIM 2010 R2 Self-Service Password Reset Auto-Registration

I have been working on our FIM 2010 R2 SP1 lab environment looking for ways to simplify some of the overly complicated scenarios we had to implement to workaround the limitations in FIM 2010. One of those workarounds was the auto-registration of SSPR for new employees. When we onboard a new employee, we want to create a simple SSPR for them to get their first-time password reset.

Prior to the R2 release we were using the client and PowerShell to complete the registration process on a daily schedule. I was working on converting this to use the R2 registration cmdlets and combined it with PowerShell 3.0 Workflows to get to a solution similar to this.
[CmdletBinding()]  param()  begin {      workflow Invoke-RegisterSSPR {          InlineScript {              Import-Module FIM          }  $workflow = InlineScript { Get-FIMResource -Filter '/WorkflowDefinition[DisplayName = "Password Reset AuthN Workflow for New Employees"]' }  $members  …

PowerShell Error Handling Behavior Debunked

Note: I am using simple error messages as an example, please reference the best practices and guidelines I outlined on when to use custom error messages.

I have been churning in my mind for the last few days all the entries in the 2013 Scripting Games and how they handle errors, or lack thereof.

I am coming to the conclusion through some testing that the simple fact of seeing a try..catch or throw statements does not mean there is proper error handling.

I've been testing several variations and forms of error handling, so lets start with the basics.
function Test-WriteError {      [CmdletBinding()] param()  "Test-WriteError::ErrorActionPreference = $ErrorActionPreference"Move-Item -Path 'C:\Does\Not\Exists.log' -Destination 'C:\No\Where'"Test-WriteError::End"}   Test-WriteError::ErrorActionPreference = Continue
Move-Item : Cannot find path 'C:\Does\Not\Exists.log' because it does not exist.
At line:6 char:5
+     Move-Item -Path 'C:\Does\N…