Skip to content

Instantly share code, notes, and snippets.

@amnich
Last active August 28, 2017 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amnich/2a9faf7648c668cf96721db5340d2bfb to your computer and use it in GitHub Desktop.
Save amnich/2a9faf7648c668cf96721db5340d2bfb to your computer and use it in GitHub Desktop.
if ((Get-Command Get-ChildItem).parameters.Keys -notcontains "File") {
function global:Get-ChildItem {
[CmdletBinding(DefaultParameterSetName = 'Items', SupportsTransactions = $true)]
param(
[Parameter(ParameterSetName = 'Items', Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string[]]
${Path},
[Parameter(ParameterSetName = 'LiteralItems', Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('PSPath')]
[string[]]
${LiteralPath},
[Parameter(Position = 1)]
[string]
${Filter},
[string[]]
${Include},
[string[]]
${Exclude},
[Alias('s')]
[switch]
${Recurse},
[uint32]
${Depth},
[switch]
${Force},
[switch]
${File},
[switch]
${Directory},
[switch]
${Name})
dynamicparam {
if ($PSBoundParameters.Path) { $GciPath = $PSBoundParameters.Path }
elseif ($PSBoundParameters.LiteralPath) { $GciPath = $PSBoundParameters.LiteralPath }
else { $GciPath = "." }
$DynParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$Parameters = Get-Command -CommandType Cmdlet -Name Microsoft.PowerShell.Management\Get-ChildItem -ArgumentList $GciPath |
Select-Object -ExpandProperty Parameters
foreach ($Parameter in ($Parameters.GetEnumerator() | Where-Object { $_.Value.IsDynamic })) {
$DynamicParameter = New-Object System.Management.Automation.RuntimeDefinedParameter (
$Parameter.Key,
$Parameter.Value.ParameterType,
$Parameter.Value.Attributes
)
$DynParamDictionary.Add($Parameter.Key, $DynamicParameter)
}
$DynParamDictionary
}
begin {
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) {
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Get-ChildItem', [System.Management.Automation.CommandTypes]::Cmdlet
)
if ($PSBoundParameters.Keys -contains ("File") -and $PSBoundParameters.Keys -notcontains ("Directory")) {
$PSBoundParameters.Remove('File') | out-null
$scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object { ! $_.PSIsContainer} }
}
elseif ($PSBoundParameters.Keys -contains ("Directory") -and $PSBoundParameters.Keys -notcontains ("File")) {
$PSBoundParameters.Remove('Directory') | out-null
$scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object { $_.PSIsContainer} }
}
elseif ($PSBoundParameters.Keys -contains ("Directory") -and $PSBoundParameters.Keys -contains ("File")) {
$PSBoundParameters.Remove('File') | out-null
$PSBoundParameters.Remove('Directory') | out-null
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
}
else {
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
}
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process {
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
end {
try {
$steppablePipeline.End()
} catch {
throw
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment