none
Editing Description Field on a Computer in AD RRS feed

  • Question

  • I am new to PowerShell, just learning. I have this script below that works fine to pull up stalled computers for 90 days and disable them then write a description to the description field.

    $DaysAgo=(Get-Date).AddDays(-90)
    $CurrentDate = Get-Date
    $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')
    $Domains = "xxxA1.xxxx.xx","xxxA2.xxx.xxx","xxxA3.xxx.xxx"

    $Computers = Get-ADComputer -Server $Domain -Filter {(PwdLastSet -lt $DaysAgo -and (OperatingSystem -Like "Windows 7*" -or OperatingSystem -like "Windows 10*"))} -Properties PwdLastSet,LastLogonTimeStamp,Description,OperatingSystem
    foreach ($computer in $computers){
               $desc="Disabled by AD-Group on $(Get-Date) - $($computer.Description)"
              
                  Set-ADComputer $Computer -Description $desc -Enabled $false
        
      }

    Now I am trying to be able to query any of these computers that I need to re-enable them back and remove the description that was put after disabling them ($desc="Disabled by AD-Group on $(Get-Date) - $($computer.Description)") but leave anything else alone in the description field. This is what I have come up with so far, but how can I remove the description part i.e. "Disabled by AD-Group 03/15/2018 12:03? Please help?

    $ComputerName = Read-Host "Enter Computer Name "

    $Domains = "xxxA1.xxx.xxx","xxxA2.xx.xxx","xxxA3.xxx.xxx"

    foreach ($Domain in $Domains) {

    Get-ADComputer -Server $Domain -Filter {Enabled -eq $false -and Name -like $ComputerName -and Description -like "Disabled by AD-Group*"} -Properties PwdLastSet,LastLogonTimeStamp,Description,OperatingSystem

              
                  Set-ADComputer $Computer -Enabled $True

      }

    Thanks!

    Thursday, July 19, 2018 9:30 PM

Answers

  • Here is the code I have. Apparently no errors but its not removing the description either..  Thanks!
    $ComputerName = Read-Host "Enter Computer Name " 
    
    $Domains = "xxxA1.xxx.xxx","xxxA2.xxx.xxx","xxxA3.xxx.xxx"
    
    foreach ($Domain in $Domains) {
    
    $Computers = Get-ADComputer -Server $Domain -Filter {Enabled -eq $False -and Name -like $ComputerName -and Description -like "Disabled by AD-Group*"} -Properties PwdLastSet,LastLogonTimeStamp,Description,OperatingSystem
    foreach ($computer in $computers){
               
               $desc = 'Disabled by AD-Group on'
               $desc -replace 'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '
    
              Set-ADComputer $Computer -Enabled $True -Description $desc
    
         
    		} 
    }

    It would be best if you learned PowerShell before trying to use it.

              $desc =$computer.Description
               $desc = $desc
    -replace 'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '
             
    Set-ADComputer $Computer -Enabled $True -Description $desc

    If you don't get the description first and save the changes nothing will change.  Simple logic.  YOu have to actually think about what you are doing when working with technology.


    \_(ツ)_/


    • Edited by jrv Friday, July 20, 2018 6:51 PM
    • Marked as answer by Canbe1444 Friday, July 20, 2018 8:01 PM
    Friday, July 20, 2018 6:51 PM
  • Wow! that worked just exactly the way I wanted it. Thanks jrv and Leif-Arne.. you guys rock!!  below is the final and complete code that works..
    $ComputerName = Read-Host "Enter Computer Name " 
    
    $Domains = "xxx01.xxx.xx","xxx02.xxx.xxx","xxx03.xxx.xxx"
    
    foreach ($Domain in $Domains) {
    
    $Computers = Get-ADComputer -Server $Domain -Filter {Enabled -eq $False -and Name -like $ComputerName -and Description -like "Disabled by AD-Group*"} -Properties PwdLastSet,LastLogonTimeStamp,Description,OperatingSystem
    foreach ($computer in $computers){
                $desc  =$computer.Description
               $desc = $desc -replace 'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '
               Set-ADComputer $Computer -Enabled $True -Description $desc
    
                  } 
    }
       

    • Marked as answer by Canbe1444 Friday, July 20, 2018 8:00 PM
    Friday, July 20, 2018 7:58 PM

All replies

  • Use replace.

    $desc -replace 'Disabled by AD-Group \d+/\d+/\d+ \d+:\d+'

    PS D:\scripts> $desc = 'Disabled by AD-Group 03/15/2018 12:03 - other stuff'
    PS D:\scripts> $desc -replace 'Disabled by AD-Group \d+/\d+/\d+ \d+:\d+:\d+ - '
     other stuff
    PS D:\scripts>


    \_(ツ)_/




    • Edited by jrv Thursday, July 19, 2018 10:16 PM
    Thursday, July 19, 2018 10:13 PM
  • Hey jrv, Thanks for your response and willing to help.  maybe I am not getting it... I tried this and getting error .. not working

    $ComputerName=Read-Host"Enter Computer Name "



    $Domains

    ="xxxA1.xxx.xxx","xxxA2.xxx.xxx","xxxA3.xxx.xxx"



    foreach

    ($Domainin$Domains) {



    $Computers

    =Get-ADComputer-Server$Domain-Filter{Enabled-eq$False-andName-like$ComputerName-andDescription-like"Disabled by AD-Group*"} -PropertiesPwdLastSet,LastLogonTimeStamp,Description,OperatingSystem


    foreach

    ($computerin$computers){

    $desc='Disabled by AD-Group on 03/15/2018 12:03 - other stuff'

    $desc-replace'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '

    otherstuff

    Set-ADComputer $Computer -Enabled $True -Description $desc

                  

      }

    ERROR

    other : The term 'other' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or

    if a path was included, verify that the path is correct and try again.

    At line:17 char:2

    +  other stuff

    +  ~~~~~

        + CategoryInfo          : ObjectNotFound: (other:String) [], CommandNotFoundException

        + FullyQualifiedErrorId : CommandNotFoundException

    When I use $desc -replace 'Disabled by AD-Group \d+/\d+/\d+ \d+:\d+' what am I replacing with so it can just remove that part? Sorry I may sound dump but I am just new to PowerShell and learning. Maybe it will help if you include your inputs to my lines on the script..

    Friday, July 20, 2018 2:54 PM
  • Please post code using the Insert Code Block button.

    $desc = 'Disabled by AD-Group 03/15/2018 12:03 - other stuff'
    $desc -replace 'Disabled by AD-Group \d+/\d+/\d+ \d+:\d+:\d+ - '

    If a string is found in $desc that matches the pattern on the right-hand side of the -replace operator, the matching part of the string will be replaced with nothing and in this case only "other stuff" should remain in $desc.

    You were not supposed to include the "other stuff" line from jrv's post. He just tried to show what you should end up with if you run the two lines above. If you do run the two lines you will notice nothing is changed in $desc. That is because $desc does not include seconds, only hours and minutes, but the pattern looks for seconds as well. 

    Friday, July 20, 2018 5:52 PM
  • Here is the code I have. Apparently no errors but its not removing the description either..  Thanks!
    $ComputerName = Read-Host "Enter Computer Name " 
    
    $Domains = "xxxA1.xxx.xxx","xxxA2.xxx.xxx","xxxA3.xxx.xxx"
    
    foreach ($Domain in $Domains) {
    
    $Computers = Get-ADComputer -Server $Domain -Filter {Enabled -eq $False -and Name -like $ComputerName -and Description -like "Disabled by AD-Group*"} -Properties PwdLastSet,LastLogonTimeStamp,Description,OperatingSystem
    foreach ($computer in $computers){
               
               $desc = 'Disabled by AD-Group on'
               $desc -replace 'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '
    
              Set-ADComputer $Computer -Enabled $True -Description $desc
    
         
    		} 
    }

    Friday, July 20, 2018 6:36 PM
  • What is the current description for one of the computers and what would you like to change it to?
    Friday, July 20, 2018 6:38 PM
  • Here is the code I have. Apparently no errors but its not removing the description either..  Thanks!
    $ComputerName = Read-Host "Enter Computer Name " 
    
    $Domains = "xxxA1.xxx.xxx","xxxA2.xxx.xxx","xxxA3.xxx.xxx"
    
    foreach ($Domain in $Domains) {
    
    $Computers = Get-ADComputer -Server $Domain -Filter {Enabled -eq $False -and Name -like $ComputerName -and Description -like "Disabled by AD-Group*"} -Properties PwdLastSet,LastLogonTimeStamp,Description,OperatingSystem
    foreach ($computer in $computers){
               
               $desc = 'Disabled by AD-Group on'
               $desc -replace 'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '
    
              Set-ADComputer $Computer -Enabled $True -Description $desc
    
         
    		} 
    }

    It would be best if you learned PowerShell before trying to use it.

              $desc =$computer.Description
               $desc = $desc
    -replace 'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '
             
    Set-ADComputer $Computer -Enabled $True -Description $desc

    If you don't get the description first and save the changes nothing will change.  Simple logic.  YOu have to actually think about what you are doing when working with technology.


    \_(ツ)_/


    • Edited by jrv Friday, July 20, 2018 6:51 PM
    • Marked as answer by Canbe1444 Friday, July 20, 2018 8:01 PM
    Friday, July 20, 2018 6:51 PM
  • The current description on the computer is "Disabled by AD-Group on 07/20/2018 14:07:40 - Server Team"

    I want to remove "Disabled by AD-Group on 07/20/2018 14:07:40" and leave "Server Team" alone. When I run this code below, it does nothing: Also the date and time is different from each computer. Thanks you guys for helping me on this.

    $ComputerName = Read-Host "Enter Computer Name " 
    
    $Domains = "xxx01.xxx.xxx","xxx2.xxx.xxx","xxx3.xxx.xxx"
    
    foreach ($Domain in $Domains) {
    
    $Computers = Get-ADComputer -Server $Domain -Filter {Enabled -eq $False -and Name -like $ComputerName -and Description -like "Disabled by AD-Group*"} -Properties PwdLastSet,LastLogonTimeStamp,Description,OperatingSystem
    foreach ($computer in $computers){
               $desc  =$computer.Description
               $desc -replace 'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '
               ADComputer $Computer -Enabled $False -Description $desc
    
          
    		} 
    }

    Friday, July 20, 2018 7:29 PM
  • Please look carefully at my last post. You did not copy it correctly.


    \_(ツ)_/

    Friday, July 20, 2018 7:34 PM
  • Wow! that worked just exactly the way I wanted it. Thanks jrv and Leif-Arne.. you guys rock!!  below is the final and complete code that works..
    $ComputerName = Read-Host "Enter Computer Name " 
    
    $Domains = "xxx01.xxx.xx","xxx02.xxx.xxx","xxx03.xxx.xxx"
    
    foreach ($Domain in $Domains) {
    
    $Computers = Get-ADComputer -Server $Domain -Filter {Enabled -eq $False -and Name -like $ComputerName -and Description -like "Disabled by AD-Group*"} -Properties PwdLastSet,LastLogonTimeStamp,Description,OperatingSystem
    foreach ($computer in $computers){
                $desc  =$computer.Description
               $desc = $desc -replace 'Disabled by AD-Group on \d+/\d+/\d+ \d+:\d+:\d+ - '
               Set-ADComputer $Computer -Enabled $True -Description $desc
    
                  } 
    }
       

    • Marked as answer by Canbe1444 Friday, July 20, 2018 8:00 PM
    Friday, July 20, 2018 7:58 PM