SOLVED

remove-item -Verbose shows on host but not on log file.

%3CLINGO-SUB%20id%3D%22lingo-sub-1766435%22%20slang%3D%22en-US%22%3Eremove-item%20-Verbose%20shows%20on%20host%20but%20not%20on%20log%20file.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1766435%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20post%20new%20to%20Powershell%20moving%20away%20from%20batch%20commands.%26nbsp%3B%20Simple%20command%20Delete%20a%20file%20and%20output%20the%20results%20to%20file(log).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20batch%20command%3A%26nbsp%3B%20%26nbsp%3Bdel%20myFile.log%20%26gt%3B%26gt%3B%20myLogFile.txt%3C%2FP%3E%3CP%3EAll%20output%20goes%20to%20the%20file.%26nbsp%3B%20Nothing%20on%20the%20screen.%20(Normal%20behavior)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPowershell%20Version%205.1%3A%20Remove-item%20test.log%20-Verbose%20%7C%20Add-content%20-path%20%22C%3A%5Ctemp%5CmyLogFile.txt%22%3C%2FP%3E%3CP%3ENo%20Output%20blank%20file.%26nbsp%3B%20%26nbsp%3B%20Verbose%20shows%20on%20the%20host%20screen.%20OK%2C%20verbose%20is%20not%20an%20object.%20Is%20it%20a%20string%20sent%20to%20the%20host%3F%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPowershell%20Version%205.1%3A%3C%2FP%3E%3CP%3ERemove-item%20test.log%20-Verbose%20*%26gt%3B%20%22C%3A%5Ctemp%5CmyLogFile.txt%22%3C%2FP%3E%3CP%3ENo%20Output%20blank%20file.%26nbsp%3B%20%26nbsp%3BOK%2C%20not%20a%20string.%26nbsp%3B%20%26nbsp%3B%20Is%20it%20a%20process%20Stream%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPowershell%20Version%205.1%3A%26nbsp%3B%20Remove-item%20test.log%20-Verbose%204%26gt%3B%26amp%3B1%20%7C%20add-content%20%22C%3A%5Ctemp%5CmyLogFile.txt%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ERemove-item%20test.log%20-Verbose%204%26gt%3B%26amp%3B1%20%7C%20add-content%20%22C%3A%5Ctemp%5CmyLogFile.txt%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOK%2C%20That%20worked.%26nbsp%3B%20The%20command%20redirects%20the%20verbose%20output%20to%20Success%20Stream%20that%20add-content%20can%20see%20as%20a%20string%20object.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20the%20question.%26nbsp%3B%20Is%20there%20a%20better%20way%20to%20code%20in%20Powershell%205.1%20to%20get%20the%20same%20results%3F%26nbsp%3B%20Can%20verbose%20redirect%20be%20written%20in%20an%20easier%20to%20read%20manor%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1766435%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1767015%22%20slang%3D%22en-US%22%3ERe%3A%20remove-item%20-Verbose%20shows%20on%20host%20but%20not%20on%20log%20file.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1767015%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F824867%22%20target%3D%22_blank%22%3E%40TampaCCT%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERead%20me%20post%20from%20here%2C%3C%2FP%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Ffarisnt.blogspot.com%2F2020%2F07%2Fpowershell-dump-console-output-to-file.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Ffarisnt.blogspot.com%2F2020%2F07%2Fpowershell-dump-console-output-to-file.html%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20explain%20this%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20related%20to%20output%20redirection%2C%3C%2FP%3E%3CP%3Euse%20*%26gt%3B%20to%20redirect%20the%20output%20from%20the%20console%20to%20the%20file%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

 

First post new to Powershell moving away from batch commands.  Simple command Delete a file and output the results to file(log).

 

In batch command:   del myFile.log >> myLogFile.txt

All output goes to the file.  Nothing on the screen. (Normal behavior)

 

Powershell Version 5.1: Remove-item test.log -Verbose | Add-content -path "C:\temp\myLogFile.txt"

No Output blank file.    Verbose shows on the host screen. OK, verbose is not an object. Is it a string sent to the host?  

 

Powershell Version 5.1:

Remove-item test.log -Verbose *> "C:\temp\myLogFile.txt"

No Output blank file.   OK, not a string.    Is it a process Stream?

 

Powershell Version 5.1:  Remove-item test.log -Verbose 4>&1 | add-content "C:\temp\myLogFile.txt"

 

Remove-item test.log -Verbose 4>&1 | add-content "C:\temp\myLogFile.txt"

 

OK, That worked.  The command redirects the verbose output to Success Stream that add-content can see as a string object.

 

Here is the question.  Is there a better way to code in Powershell 5.1 to get the same results?  Can verbose redirect be written in an easier to read manor?

 

 

1 Reply
Best Response confirmed by TampaCCT (New Contributor)
Solution

@TampaCCT 

Read my post from here,

https://www.powershellcenter.com/2020/08/14/powershell-dump-the-console-output-to-a-file-output-redi...

 

I explain this 

This is related to output redirection,

use *> to redirect the output from the console to the file

 

This is a stream, and I don't think there is a better way to redirect the stream in the console to a file, as usually verbose are meant for troubleshooting only, 

 

 

-------------------

If this answer seems good to you, please click on Best Response

Thanks