I'm writing a small script that checks for the existence of directories, and if they exist then delete them. I am trying to have informative output, and as such I want to output a confirmation when a folder is deleted:
foreach ($e in $directoriesToRemove)
{
Write-Output "Deleting $e`..."
Remove-Item $e -recurse
Write-Output "Done"
}
The output of this being:
Deleting C:\wherever\folder1...
Done
Deleting C:\wherever\folder2...
Done
While I am aiming for:
Deleting C:\wherever\folder1... Done
Deleting C:\wherever\folder2... Done
I understand that Write-Host can be used to achieve this with the -nonewline parameter, but I would like to avoid using Write-Host if possible.
Is there a workaround for this other than waiting until a folder is deleting and writing the entire line at once? I've found that this line works, but I don't know if putting a delete command inside a Write-Output command is safe/best practice:
Write-Output "Deleting $e`... $(Remove-Item $e -recurse) Done"
write-outputis executed.