2

I have a script with a series of Invoke-Command statements, and each one has code on the end writing out to a file. Can I simply change the behavior of Out-Default temporarily and clean up my script a little?

... | Out-File $LogFile -append

[EDIT] This is on Powershell 2.0

1
  • 1
    If you create a command or alias called Out-Default, that does what you want it to, will that replace the default Out-Default? Aug 6, 2013 at 16:11

4 Answers 4

6

Can you? Yes.

filter Out-Default{ $_ | Out-File 'C:\logfile.txt' -append }

Now all output will go to the log file by default. Remove the temporary definition via

dir function:\Out-Default | del

But this is very hacky, non-obvious, and difficult to maintain, so I would not recommend it. It would be better to define a simple dedicated logging function, so that you only needed to add | log to the relevant lines of the script. Much better to have a bit of extra code that is obvious to understand, debug, and change, than to add hacks just to "simplify" the code.

2
  • Thanks latkin, that sounds like the best solution.
    – Ben Finkel
    Aug 7, 2013 at 13:59
  • Nice. The command for removing the function can be simplified to: Remove-Item Function:Out-Default
    – mklement0
    Sep 20, 2019 at 16:34
6

I wouldn't change the way Out-Default works, if you have PowerShell 3.0 you can set default parameter values for Out-File, doing so will not require you to specify them on the command line:

$PSDefaultParameterValues['Out-File:FilePath'] = $LogFile
$PSDefaultParameterValues['Out-File:Append'] = $true


... | Out-File
1
  • Thanks, that's a lot better, but only on v2.0 unfortunately.
    – Ben Finkel
    Aug 6, 2013 at 14:55
1

From "Powershell Cookbook", 2013:

<#  
.SYNOPSIS
Adds a new Out-Default command wrapper to store up to 500 elements from
the previous command. This wrapper stores output in the $ll variable.
#>

Set-StrictMode -Version 3

New-CommandWrapper Out-Default `
-Begin {
    $cachedOutput = New-Object System.Collections.ArrayList
} `
-Process {
    if($_ -ne $null) { $null = $cachedOutput.Add($_) }
    while($cachedOutput.Count -gt 500) { $cachedOutput.RemoveAt(0) }
} `
-End {
    $uniqueOutput = $cachedOutput | Foreach-Object {
        $_.GetType().FullName } | Select -Unique

    $containsInterestingTypes = ($uniqueOutput -notcontains `
    "System.Management.Automation.ErrorRecord") -and
    ($uniqueOutput -notlike `
    "Microsoft.PowerShell.Commands.Internal.Format.*")

    if(($cachedOutput.Count -gt 0) -and $containsInterestingTypes)
    {
        $GLOBAL:ll = $cachedOutput | % { $_ }
    }
}
1
0

A less obscure alternative to overriding the Out-Default cmdlet for implicit Out-File calls is to wrap the commands of interest in a script block ({ ... }) and pipe its invocation (&) to a single Out-File call.

Unless you need to control the output encoding, you can then even use >, Out-File's effective alias.

& {

  # ... Put the commands of interest here; e.g.:

  'hi'

  Get-Item /

  Get-Date 

  # ...

} > $LogFile  
# If you need to control the output encoding, use
# `| Out-File $LogFile -Encoding ...` instead of `> $LogFile`

On a general note: files created with > / Out-File capture for-display representations that aren't generally suitable for programmatic processing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.