Forum Discussion
MicheleSWFWMD
Mar 20, 2023Copper Contributor
How to get output to file from Remove-Item
Very new to Powershell. I have a script that goes through a folder and removes files not accessed within 14 days. I have been unable to get the output of removed files to write to a file. Get-Chi...
CutlerTS
Mar 20, 2023Brass Contributor
I'm not an expert in PowerShell, but I don't see write-host (or any output statements/flags) within the foreach statement.
- MicheleSWFWMDMar 20, 2023Copper ContributorThank you, yes, I have tried Output-File within the brackets as well as redirection "*>" and out side the brackets after the foreach. I posted the working script which outputs to the console, Just need to find how to add the file to log to.
- CutlerTSMar 20, 2023Brass ContributorI Googled up a solution. It appears that it can't output within the loop. Rather, it suggest piping the whole thing, like maybe this way:
& {
foreach {remove-item $_.fullname -recurse -force -verbose -whatif}
} | Out-File MyLog.txt -Append
You can just Google "powershell foreach log to file" to find different solutions that best fits your need.- MicheleSWFWMDMar 20, 2023Copper ContributorThank you, I will try that. I have been down the Google trail and wasn't able to get any to work. I will continue to test.