Search code examples
powershellloggingwarningsverbose

How can I log everything that is written in powershell window?


I am needing to create a logfile for every action that is performed by this script.

I have attempted to use redirection(>>) however, cannot get the output to actually write to the file.

while($true){

$FromMC = Get-ChildItem -Path "\\x\From MC" -Filter *.mpg
Write-Host "Checking '\\x\From MC' for files" -ForegroundColor Cyan
Write-Host $FromMC.count"Files Found" -ForegroundColor Cyan
Write-Host ""
ForEach($file in $FromMC){

    try{
        Move-Item -Filter 7ST* -Path $file.Fullname -Destination "\\x\programs\7TH STREET THEATER" -Verbose -Force -ErrorAction Stop
    }
    catch{...}
Write-Host "Pausing for"$ts.Minutes"minutes..." -ForegroundColor Cyan
Write-Host "Ctrl+C to Stop"
Write-Host ""
Start-Sleep -Seconds $ts.TotalSeconds
}

I expect the output to be exactly as "-verbose" outputs into the shell. Types of output: Write-Host, Verbose, Write-Warning

I feel the solution is extremely simple, and I am just overlooking it.


Solution

  • To log everything that would normally be written to the console, like -verbose you can use Start-Transcript and when you are finished Stop-Transcript.

    Example:

    Start-Transcript -Path "C:\logs.txt"
    
    #run code you want to capture
    
    Stop-Transcript