Thanks to Don and Dave I was able to get the full UNC path of files in a network location. (see code above). Now with this text file, i will be deleting all the files that are listed using the code below. Everything works fine but i would like to log the files that are being deleted and in case if it fails for some reason, i’d be able to see which files and why. How do i output Remove-Item to text? Please advise.
Hello Dave, I was able to get it it to output by using the code below. This script is to be run on the background so no status messages needs to be displayed.
It works perfectly fine with PowerShell 4.0. However, when I copied it to the server, I received the attached error. After some digging, I found out that it’s a version issue. The version that is currently installed on the server is PowerShell 2.0.
http://technet.microsoft.com/en-us/library/hh847746.aspx
NOTE: The All (*), Warning (3), Verbose (4) and Debug (5) redirection operators were introduced
in Windows PowerShell 3.0. They do not work in earlier versions of Windows PowerShell.
How do I get the same results using Powershell 2.0 with the code I provided above?
Do you need Verbose, Warning or Debug output in the log file, or only errors? If it’s errors only, you can use the 2>> operator in PowerShell 2.0 (instead of *>> in your example).
I don’t think I’m understanding your question, then. I suggested code which outputs information when Remove-Item successfully deletes something, and when it produces errors, but you went in a different direction with the redirection operators. Redirection would only give you verbose / debug / error / warning output, since Remove-Item doesn’t produce anything on the Output stream by itself.
I needed to see the what Remove-Item does. Which is why i used -verbose to which i used *>> operator in order to capture it and output it to text. This is what i get in the text file 2014-04-07.txt
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\20amp.txt”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Capture.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Capture1.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Capture2.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Capture3.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\PhotomergeUI.8BF”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\public_drive_log.txt”.
Remove-Item : Cannot find path ‘\cd1001-c100\public\isantillan\Thumbs.db’ because it does not exist.
At line:2 char:77
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture2.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture3.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture4.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture5.PNG”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\folder1\Robocopy.exe”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\folder2\Robocopy.exe”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\folder3\Robocopy.exe”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Copier_unstoppable\UnstopCpy_5_2_Win2K_UP.exe”.
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Copier_unstoppable\UnstopCpy_Configure.jpg”.
Remove-Item : Cannot find path ‘\cd1001-c100\public\isantillan\Copier_unstoppable\Thumbs.db’ because it does not exist.
At line:2 char:77
It may also be possible to make use of the System.Management.Automation.PowerShell class to run your command, which would get you access to all of the stream output from the commands executed by that PowerShell object. Other than those options, I don’t think there’s any other way you can intercept and log Verbose stream output prior to PowerShell 3.0.
Will this work in powershell 2.0?. Add this function to you script. It should capture the calls to write-verbose. I do not have access to anything running powershell 2.0, so I cannot test it:
That works for intercepting explicit calls to the Write-Verbose cmdlet, but it would not catch verbose output from compiled cmdlets such as Remove-Item. That type of output goes through the Cmdlet.WriteVerbose() method.