PoshCode Logo PowerShell Code Repository

Get-ChildItemProxy by Andy Schneider 5 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/785"></script>download | new post

This is an advanced function that uses a proxy command to add two new switches to get-childitem
You can name this function get-childitem and it will overwrite the existing cmdlet.
With Proxy Commands, you can mess with the meta data and add/subtract parameters from real cmdlets
Most of this code was generated with Jeffrey Snover’s new Metaprogramming Module
http://blogs.msdn.com/powershell/archive/2009/01/04/extending-and-or-modifing-commands-with-proxies.aspx
http://www.get-powershell.com

  1. Function Get-ChildItemProxy {
  2. [CmdletBinding(DefaultParameterSetName='Items', SupportsTransactions=$true)]
  3. param(
  4.     [Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  5.     [System.String[]]
  6.     ${Path},
  7.  
  8.     [Parameter(ParameterSetName='LiteralItems', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
  9.     [Alias('PSPath')]
  10.     [System.String[]]
  11.     ${LiteralPath},
  12.  
  13.     [Parameter(Position=1)]
  14.     [System.String]
  15.     ${Filter},
  16.  
  17.     [System.String[]]
  18.     ${Include},
  19.  
  20.     [System.String[]]
  21.     ${Exclude},
  22.  
  23.     [Switch]
  24.     ${Recurse},
  25.  
  26.     [Switch]
  27.     ${Force},
  28.  
  29.     [Switch]
  30.     ${Name},
  31.    
  32.     [Switch]
  33.     ${ContainersOnly},
  34.    
  35.     [Switch]
  36.     ${NoContainersOnly}
  37.     )
  38.  
  39. begin
  40. {
  41.     try {
  42.         $outBuffer = $null
  43.         if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer) -and $outBuffer -gt 1024)
  44.         {
  45.             $PSBoundParameters['OutBuffer'] = 1024
  46.         }
  47.         $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-ChildItem', [System.Management.Automation.CommandTypes]::Cmdlet)
  48.        
  49.         if ($ContainersOnly)
  50.         {
  51.             [Void]$PSBoundParameters.Remove("ContainersOnly")
  52.             $scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object {$_.PSIsContainer -eq $true}}
  53.            
  54.         } elseif ($NoContainersOnly)
  55.                {
  56.                    [Void]$PSBoundParameters.Remove("NoContainersOnly")
  57.                    $scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object {$_.PSIsContainer -eq $false}}
  58.                }    
  59.         {
  60.             $scriptCmd = {& $wrappedCmd @PSBoundParameters }
  61.         }
  62.        
  63.  
  64.        
  65.         $steppablePipeline = $scriptCmd.GetSteppablePipeline()
  66.         $steppablePipeline.Begin($PSCmdlet)
  67.     } catch {
  68.         throw
  69.     }
  70. }
  71.  
  72. process
  73. {
  74.     try {
  75.         $steppablePipeline.Process($_)
  76.     } catch {
  77.         throw
  78.     }
  79. }
  80.  
  81. end
  82. {
  83.     try {
  84.         $steppablePipeline.End()
  85.     } catch {
  86.         throw
  87.     }
  88. }
  89. <#
  90.  
  91. .ForwardHelpTargetName Get-ChildItem
  92. .ForwardHelpCategory Cmdlet
  93.  
  94. #>
  95.  
  96. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me