fmsrv
fmsrv

Reputation: 123

how to get -verbose output to a text file in powershell

I want to get the verbose result to a log file in powershell, I m able to see the verbose line in shell but unable to get it into variable or log file

I have used Tee-Object and given the file path but it dont seems to be working

Move-Item $source $_.File_Destination_Path -Force -Verbose | tee -Append -FilePath $Logfile

Expected output in logfile- "VERBOSE: Performing the operation "Move File...." But the result is blank, Please help me on this

Upvotes: 3

Views: 3815

Answers (2)

DarkLite1
DarkLite1

Reputation: 14755

Another way to have all the Write-Verbose messages in a text file is by using Start-Transcript as explained here. This is especially useful for debugging PowerShell jobs.

$job = Start-Job -ScriptBlock {
    $params = @{
        Path      = 'C:\Temp\{0} transcript.txt' -f (Get-Date).ToString('yyyyMMddHHmmss')
        NoClobber = $true
    }
    Start-Transcript @params

    $VerbosePreference = 'Continue'

    # not in the text file
    Get-ChildItem 
    Write-Output 'this is output'

    # in the text file:
    Write-Verbose 'this is verbose' 
}

$job | Wait-Job

Upvotes: 1

Maximilian Burszley
Maximilian Burszley

Reputation: 19704

As comments have mentioned, you should check out the about_Redirection msdn topic. In practice, it would look like:

Move-Item -Path $source -Destination $_.File_Destination_Path -Force -Verbose 4>&1 |
    Tee-Object -Append -FilePath $Logfile

Note the VERBOSE: bit is added by and you would need to append it yourself to have it visible in the output file.

Upvotes: 3

Related Questions