PowerShell: Modifying AD Information

2 min readGinja

I have a file full of commandlets that I share with my co-workers and I'm going to start sharing some of those functions here.   I will explain how it works and how it can be used. I hope this will also help me improve upon them and tweak them where needed.

This commandlet/function will modify the specified AD attributes of a user.

function Update-GeneralAdInfo {

param ($usrName)

    Write-Host "-------" -fore gray
    Write-Host "Change:" -fore red
    Write-Host "-------" -fore gray
    Write-Host " "
    Write-Host "1. Title/Description"
    Write-Host "2. Department"
    Write-Host "3. Manager"
    Write-Host "4. Office"
    Write-host "--------------------" -fore gray
   
    $selectOp = read-host "Choose a number"
   
    Switch ($selectOp) {
   
    "1" {
   
        $newTitle = Read-Host "New Title/Description"
        Set-QADUser $usrName -Title $newTitle -Description $newTitle -credential $identity -confirm
   
    }
   
    "2" {
   
        $newDepartment = Read-Host "New Department"
        Set-QADUser $usrName -Department $newDepartment -credential $identity -confirm
   
    }
   
    "3" {
   
        $newManager = Read-Host "New Manager"
   
        if (Get-QADUser $newManager) {
   
            Set-QADUser $usrName -Manager $newManager -credential $identity -confirm
        }   
    }
   
    "4" {
   
   
        $newOffice = Read-Host "New Office/Location?"
       
        if ($newOffice) {
   
            Set-QADUser $usrName -Office $newOffice -credential $identity -confirm
       
        }
   
   }
 }
}

To run this you'd type:

Update-GeneralAdInfo robertsm

Robertsm is the specified account's Sam account name.
Some things worth noting:
My script is made to be run as a regular user account (no domain credentials), so when it launches it prompts you for your credentials that have domain access. 
Here is the code I use to do that:

if ($identity -eq $null) {
    $user     = (Get-ChildItem env:UserName).Value
    $domain   = (Get-ChildItem env:UserDomain).Value
    $identity = Get-Credential "$domain\a$user"
   
    Write-Host "--------------------------" -fore Gray
    Write-Host "   Identity variable set! " -fore Red
    Write-Host "   Have fun, $user        " -fore blue
    Write-Host "--------------------------" -fore Gray
   
} else {

    Write-Host "--------------------------" -fore Gray
    Write-Host "Identity variable already set!" -fore Red
    Write-Host "--------------------------" -fore Gray
   
}

I also have it import the Quest AD PowerShell snap in. I use this code to both check for its existance and import it if it does:

$snapin = get-pssnapin -name "*quest*" -ea SilentlyContinue
    if ($snapin.name -ne "quest.activeroles.admanagement") {
   
        Write-Host "--------------------------" -fore Gray
        Write-Host "Loading Quest AD PS Snapin" -fore Red
        Write-Host "--------------------------" -fore Gray
       
        Add-PSSnapin Quest.ActiveRoles.ADManagement | Out-Null
       
    } else {
   
        Write-Host "--------------------------" -fore Gray
        Write-Host "Quest AD Tools already loaded!" -fore Red
        Write-Host "--------------------------" -fore Gray
       
    } if(!$?) {
   
        Write-Host "You need to install QAD Snapin from
http://www.quest.com/powershell/activeroles-server.aspx"
        Break;
       
}

The URL in that code is where you can download the Quest snap in. I find their commandlets very helpful and use them a lot in my scripting.

Comments

No comments yet. Be the first!