Skip to content

Instantly share code, notes, and snippets.

@xcud
Created February 14, 2013 19:15
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 xcud/4955455 to your computer and use it in GitHub Desktop.
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
<#
.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