Need output of this on a text file Plz help urgent.

$Computers = Get-Content D:\Scripts\Computers.txt -ReadCount 0
foreach ($Computer in $Computers)
{

Remove-ADComputer -Identity $Computer -Confirm: $false

}  

Need output of this on a text file Plz help urgent.

Please don’t put “urgent” on posts. Everyone here is a volunteer and will try to help as best they can, but we aren’t paid consultants - we have jobs also, and must focus on those first. If something is urgent, you should consider hiring a consultant.

Look at the help for the Out-File cmdlet. It should be of some use.

Sorry Don, I just had to do something quickly, Thats why use urgent, sorry about that, Tried out-file but cant get the out put in the text can you help please

What did you try?

$Computers = Get-Content D:\Scripts\Computers.txt -ReadCount 0
foreach ($Computer in $Computers)
{

Get-Adcomputer -Identity $Computer |Remove-ADComputer -Identity $Computer -Confirm: $false |Out-File C:\Users\xx\pp.txt

}

Tried this as well, not working :frowning:

Remove the -ReadCount parameter. It’s not doing anything for you.

You have to be more explicit than “it’s not working”. That’s really not helpful. What’s not working? It’s not deleting the computer accounts? Or you’re not seeing output in your file?

By the way, if you check the help file for Remove-ADComputer, there is no output. And even if there were, you would only see the last entry since you are not using the pipeline and you are not using the -Append switch.

So what are you seeing and/or not seeing that has you stumped?

I hope you are testing this in a test environment first. PowerShell is a double-edged sword and AD is a good place to create a “career limiting move” if you don’t know what you’re doing.

Thanks Bob for your reply, Yes its deleting the computer account but I cant see the output in the text file. I tried using Add-content as well but still no go.

And yes of course I am testing it on dev env only. :slight_smile:

OK, what are you putting into your Add-Content statement? Can you post your entire current script?

$Computers = Get-Content D:\Scripts\Computers.txt -ReadCount 0
foreach ($Computer in $Computers)
{

Remove-ADComputer -Identity $Computer -Confirm: $false |Add-Content -Path C:\logs.txt

}

Thats what I am using now, Before that tried using out-file as well, Its deleting the Computer accounts but not generating the output in the text file. Can you please let me know where I am going wrong ?

Thanks in advance.

As I mentioned above, there is no output from the Remove-ADComputer cmdlet and no -PassThru switch. So there is nothing in the pipeline for Add-Content to absorb. Pipeline’s empty - no content.
You would have to add another line in your foreach block.

"Deleted $Computer" | Add-Content -Path .\logs.txt"

That’s logging that you created, not any text from the Remove-ADComputer cmdlet.

Thank you so much for your help Bob, Its really helpful. There is a lot to learn I know, Thanks once again.