I need to create a PowerShell script that fixes several AD accounts in the office field with the new office spelling, all at once, from capital to normal case. I do not need change the normal office spelling for those that are correctly spelled. Using the

Martinez, Martin 0 Reputation points
2024-12-12T17:36:12.08+00:00

I need to create a PowerShell script that fixes several AD accounts in the office field with the new office spelling, all at once, from capital to normal case. I do not need change the normal office spelling for those that are correctly spelled. Using the -replace command or something else.

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,584 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,481 Reputation points
    2024-12-14T20:47:57.0466667+00:00

    This is the code submitted by @Erik Moreau ,corrected to update only those users whose spelling and/or capitalization of the "Office" property is not what is desired:

    # Define the incorrect and correct office spellings
    $incorrectOffice = "vienna, VA"     # should be spelled exactly -- CAPITALIZATION DOESN'T MATTER!
    $correctOffice   = "Vienna, VA"     # should be spelled correctly, WITH CORRECT CAPITALIZATION
    
    if ($incorrectOffice -ceq $correctOffice){
        Throw "Spelling and capitalization of both office strings is identical. This is wrong."
    }
    
    # Get all AD users with the office capitalized correctly OR incorrectly
    # and update the office field
    Get-ADUser -filter "Office -eq $incorrectspelling" -Properties Office | 
        Where-Object {$_.Office -cne $correctOffice} |
            ForEach-Object{
                Set-ADUser -Identity $_ -Office $correctOffice
                Write-Host "Updated office field for user: $($_.SamAccountName)"
            }
    Write-Host "Office field update completed."
    
    1 person found this answer helpful.
    0 comments No comments

  2. Erik Moreau 816 Reputation points MVP
    2024-12-12T18:34:26.3066667+00:00

    Hi Martin,

    if I understand your request correctly, this might be close to what you are looking for:

    # Import the Active Directory module
    Import-Module ActiveDirectory
    
    # Define the incorrect and correct office spellings
    $incorrectOffice = "OFFICEOLD"
    $correctOffice = "OfficeNew"
    
    # Get all AD users with the incorrect office spelling
    $users = Get-ADUser -Filter {Office -eq $incorrectOffice} -Properties Office
    
    # Loop through each user and update the office field
    foreach ($user in $users) {
        Set-ADUser -Identity $user -Office $correctOffice
        Write-Host "Updated office field for user: $($user.SamAccountName)"
    }
    
    Write-Host "Office field update completed."
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.