Hello there,
I am trying to delete specific folders contained in C:\temp but not all, I have listed them in a text file.
$list = gc C:\tmp\list.txt
foreach($item in $list){
Remove-Item “C:\tmp$($item)” -Recurse -Verbose
}
I tried to save the code as test.ps1 and did below, and saved the verbose stream to log
.\test.ps1 3>&1 2>&1 | Out-file log.txt
Is there any other way to delete and save the log at once in a script itself?
Olaf
April 9, 2021, 8:54pm
2
So your code seem to work, right. What’s wrong with it? Why do you want another way?
You could add some code into the script to log a little more explicitly. When you put something like this inside the loop for example:
"Removing folder '$(C:\tmp$($item))'" | Out-File -FilePath .\log.txt -Append
Thanks @Olaf . tried this , but getting an error as File can’t be opened as it is used by another process.Code is mentioned below:
function cln-tmp{
$list = gc C:\tmp\list1.txt
$i= 1 ;
$results = foreach($item in $list){
Write-Host “`n $($i). Working with [C:\tmp$($item)]…”
“`n $($i). Removing folder ‘(C:\tmp$($item))’” | Out-File -FilePath c:\tmp\log.txt -Append
Remove-Item “C:\tmp$($item)” -Recurse -Verbose
$i++
}
}
cln-tmp 3>&1 2>&1 4>&1 6>&1|Out-File c:\tmp\log.txt
So, I would like to save the folder that I am working on (can be write-host command) and the verbose results, how can I save all together to a text file ?
Olaf
April 9, 2021, 10:01pm
4
Now you’re trying to write the log from inside the script AND from outside the script - that’s why you get an error. You will have to either choose one the ways or use different files inside and outside.
1 Like