Created
February 14, 2013 19:15
-
-
Save xcud/4955455 to your computer and use it in GitHub Desktop.
Extend Get-Content to include a -Fast parameter; much less flexible but much more performant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Extend Get-Content to include a -Fast parameter; much less flexible but more performant | |
.Example | |
PS> (Measure-Command { Get-Content .\data.xml} ).TotalMilliseconds | |
1609.3267 | |
PS> (Measure-Command { Get-Content .\data.xml -Fast} ).TotalMilliseconds | |
39.2511 | |
#> | |
function Get-Content { | |
[CmdletBinding(DefaultParameterSetName='Path', SupportsTransactions=$true, HelpUri='http://go.microsoft.com/fwlink/?LinkID=113310')] | |
param( | |
[Parameter(ValueFromPipelineByPropertyName=$true)] | |
[long] | |
${ReadCount}, | |
[Parameter(ValueFromPipelineByPropertyName=$true)] | |
[Alias('First','Head')] | |
[long] | |
${TotalCount}, | |
[Parameter(ValueFromPipelineByPropertyName=$true)] | |
[Alias('Last')] | |
[int] | |
${Tail}, | |
[Parameter(ParameterSetName='Path', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)] | |
[string[]] | |
${Path}, | |
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)] | |
[Alias('PSPath')] | |
[string[]] | |
${LiteralPath}, | |
[string] | |
${Filter}, | |
[string[]] | |
${Include}, | |
[string[]] | |
${Exclude}, | |
[switch] | |
${Force}, | |
[Parameter(ValueFromPipelineByPropertyName=$true)] | |
[pscredential] | |
${Credential}, | |
[switch] | |
${Fast} | |
) | |
begin | |
{ | |
try { | |
$outBuffer = $null | |
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) | |
{ | |
$PSBoundParameters['OutBuffer'] = 1 | |
} | |
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-Content', [System.Management.Automation.CommandTypes]::Cmdlet) | |
if($PSBoundParameters['Fast']) | |
{ | |
$scriptCmd = {& Invoke-Command { [System.IO.File]::ReadAllText($PSBoundParameters['Path']) } } | |
} | |
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 | |
} | |
} | |
<# | |
.ForwardHelpTargetName Get-Content | |
.ForwardHelpCategory Cmdlet | |
#> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment