Output results of Remove-ADComputer

Rusty Peterson 21 Reputation points
2020-08-14T17:23:24.413+00:00

I need to remove a list of computers from Active Directory. I also need the results to be output to a log or text file. The remove command works and a file is generated, but the generated file is empty.

I am using the following command:
Remove-ADComputer "<ComputerName>" -Confirm:$False -Verbose | Out-File -FilePath "<FilePath>\RemovedComputers.txt" -Append

Any help would be appreciated.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,149 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,445 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,596 Reputation points
    2020-08-14T19:38:43.42+00:00

    The -Verbose output uses stream #4, if that's what you're after. The Remove-Computer cmdlet doesn't usually write anything to the normal Success stream (#1) so there's nothing for the Out-File to write. See help about_Redirection.

    You could eliminate the Pipe and the Out-File and add "4>&1 >> RemovedComputers.Txt" after the "-Verbose" parameter.

    Or, if you just want to know which computers were removed, and which computers were not removed (and why they weren't), you could try something like this:

    Try {
        Remove-ADComputer "<ComputerName>" -Confirm:$False -Verbose -ErrorAction STOP
        "Removed '<ComputerName>'" | Out-File -FilePath "<FilePath>\RemovedComputers.txt" -Append
    }
    Catch{
        "Failed to removed '<ComputerName>'" | Out-File -FilePath "<FilePath>\RemovedComputers.txt" -Append
        $_ | Out-File -FilePath "<FilePath>\RemovedComputers.txt" -Append     # writes the exception
    }
    

1 additional answer

Sort by: Most helpful
  1. Vicky Wang 2,646 Reputation points
    2020-08-17T07:01:42.943+00:00

    Hi,
    I am glad to hear that your issue was successfully resolved.
    If there is anything else we can do for you, please feel free to post in the forum.
    Have a nice day!

    0 comments No comments