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

This topic contains 10 replies, has 3 voices, and was last updated by Profile photo of Partho Sankar Roy Partho Sankar Roy 1 year, 10 months ago.

  • Author
    Posts
  • #29480
    Profile photo of Partho Sankar Roy
    Partho Sankar Roy
    Participant

    $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.

  • #29482
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #29486
    Profile photo of Partho Sankar Roy
    Partho Sankar Roy
    Participant

    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

  • #29487
    Profile photo of Don Jones
    Don Jones
    Keymaster

    What did you try?

  • #29489
    Profile photo of Partho Sankar Roy
    Partho Sankar Roy
    Participant

    $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 🙁

  • #29494
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    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.

  • #29497
    Profile photo of Partho Sankar Roy
    Partho Sankar Roy
    Participant

    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. 🙂

  • #29499
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

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

  • #29500
    Profile photo of Partho Sankar Roy
    Partho Sankar Roy
    Participant

    $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.

  • #29501
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    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.

  • #29503
    Profile photo of Partho Sankar Roy
    Partho Sankar Roy
    Participant

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

You must be logged in to reply to this topic.