Some articles are posted as Images, Please use Computers to go through them for best experience. For phone users, switch to Web Version

AD Script - Fetching AD Infra Details

<#Created By Abhishek Bansal
Read Note 

Script Usage :: Being working as Active Directory Admin & that too for multiple clients, i always have curiosity of getting an overview of their environment. So for this, i created below script which can give a high level overview of their Active Directory Environment.

Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1". Once saved, run it as Administrator.

Execution & Outputs :: During  execution, provide Client name when prompted. Output will be saved in a file named "Output.html" onto same location.
You will get to know about things like which all DC's are holding FSMO roles, Domain / Forest functional level, Total sites, Total Windows servers along with version wise count. #>


$client = Read-Host "Enter Client name = "
$detailOutput = "<html><body>
<h1>$client - Environment Info - $(Get-Date) </h1>
<table border=1 width=35% style='float: left'>"
$domaininfo = Get-ADDomain
$forestinfo = Get-ADForest
$detailOutput += "<tr>
<td width=15%><b>Fields</b></td>
<td width=20%><b>Values</b></td></tr>

<tr>
<td>Forest</td>
<td>$($domaininfo.Forest)</td></tr>

<tr>
<td>Root Domain</td>
<td>$($forestinfo.RootDomain)</td></tr>

<tr>
<td>Domain SID</td>
<td>$($domaininfo.DomainSID)</td></tr>

<tr>
<td>Forest Functional Level</td>
<td>$($forestinfo.ForestMode)</td></tr>

<tr>
<td>Domain Functional Level</td>
<td>$($domaininfo.DomainMode)</td></tr>

<tr>
<td>PDCEmulator Holder</td>
<td>$($domaininfo.PDCEmulator)</td></tr>

<tr>
<td>RIDMaster Holder</td>
<td>$($domaininfo.RIDMaster)</td></tr>

<tr>
<td>InfrastructureMaster Holder</td>
<td>$($domaininfo.InfrastructureMaster)</td></tr>

<tr>
<td>Schemamaster Holder</td>
<td>$($forestinfo.SchemaMaster)</td></tr>


<tr>
<td>DomainNamingMaster Holder</td>
<td>$($forestinfo.DomainNamingMaster)</td></tr>

<tr>
<td>Total Site count</td>
<td>$((Get-ADReplicationSite -Filter *).count)</td>
</tr>
"

$inventoryouput = "<table border=1 width=30% style='float: left;margin-left:100px'>"
$inventoryouput += "<tr>
<td><b>Fields</b></td>
<td><b>Values</b></td></tr>

<tr><td>Windows Servers Count</td>
<td>$((Get-ADComputer -Properties OperatingSystem  -Filter {OperatingSystem -like "Windows Server*"}).count)</td></tr>

<tr><td>Domain Controllers Count </td>
<td>$((Get-ADDomainController -Filter *).count)</td></tr>"


$osflavours = Get-ADComputer -Properties OperatingSystem  -Filter {OperatingSystem -like "Windows Server*"} | Select Operatingsystem -Unique

foreach($version in $osflavours)
{
$inventoryouput += "<tr><td>$($version.Operatingsystem)</td>"
$os = $((Get-ADComputer -Properties OperatingSystem -Filter * | ?{$_.Operatingsystem -eq $version.Operatingsystem})).Name
$inventoryouput += "<td>$($os.count)</td></tr>"


}

$detailOutput += "</table>"
$detailOutput | Out-File .\Output.html -Append


$inventoryouput += "</table></body></html>"
$inventoryouput | Out-File .\Output.html -Append






Output snap for reference -







AD Script - Adding multiple Users in an AD Group

<#Created By Abhishek Bansal

Read Note 

Script Usage :: Adding multiple Users in an AD Group.

Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1". On same location create a text file named "Inputusers.txt" which will be containing samaccount of all the users who will be added. Once saved, run script as Administrator.

Execution & Outputs :: During execution, it will prompt for AD group name. Need to provide the same. Once done, script will firt backup current membership of AD Group into Backup_membership.csv and final Output into Output.csv" onto same location. #>



$inputusers = Get-Content .\Inputusers.txt

$line = 0

$lines = $inputusers.count

$grp = Read-Host "Enter AD Group Name = "

Write-Host -ForegroundColor Yellow "`nAD Group Name Provided : $grp`n"

if((Get-ADGroup -Identity $grp).Samaccountname -eq $grp)

{

$choice = $(Write-Host -ForegroundColor Yellow "`nDo you want to proceed adding users mentioned in Inputusers.txt file to $grp. Press Y for Yes and N for No = " -NoNewline ; Read-Host)

Switch($choice)

{

Y {

 Write-Host -ForegroundColor Green "Backing up $grp Group membership before doing changes in Backup_membership.csv file.."

 Get-ADGroupMember -Identity $grp | Select Name,SamAccountName | Export-Csv .\Backup_membership.csv -NoTypeInformation

foreach($row in $inputusers)

{

    $line++

    $pct = $line/$lines * 100

    $row = $row.trim()

     Write-Progress -Activity "Adding Users ..." -PercentComplete $pct -Status "$line of $lines"

   try

    {

    Add-ADGroupMember -Identity $grp  -Members $row -Confirm:$false 

    $row | Select-Object -Property @{n="User ID";e={("$row")}},@{n="Status";e={("Added now.")}} | Export-Csv .\Output.csv -NoTypeInformation -Append

    }

    catch

    {

    $row  | Select-Object -Property @{n="User ID";e={($row)}},@{n="Status";e={($error.exception.Message)}} | Export-Csv .\Output.csv -NoTypeInformation -Append

    }


}

} #Closing Y condition

N {

Write-Host -ForegroundColor Green "You pressed N & hence nothing added in $grp AD Group."

} #Closing N condition

Default

{

Write-Host -ForegroundColor Red "Invalid Choice. Try again"

}

} #Closing switch

}

AD Script - Fetching User details from their Email ID's as Input

<#Created By Abhishek Bansal

Read Note 

Script Usage :: Fetching user details such as Name,Samaccountname,Accountstatus from Input file containing User Email address.

Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1". On same location create a text file named "Input.txt" which will be containing user's email address. Once saved, run script as Administrator.

Execution & Outputs :: Once executed Output will be saved in a file named "Userdetailsfromemail.csv" and any Errors into "Errorslogs.csv" onto same location.#>


$emaildata = Get-Content ./Input.txt

$line = 0

$linecount = $emaildata.Count

$pct

foreach($emailid in $emaildata)

    {

    $error.Clear()

    $line++

    $pct = $line/$linecount * 100

    $emailid = $emailid.trim()  

    Write-Progress -Activity "Fetching User account information.." -PercentComplete $pct -Status "$line of $linecount"

    if($(Get-ADUser -Filter{EmailAddress -eq $emailid}))

    {

    Get-ADUser -Properties * -Filter{EmailAddress -eq $emailid} | Select Name,Samaccountname,EmailAddress,@{n="AccountStatus";e={$_.Enabled}} | Export-Csv ./Userdetailsfromemail.csv -NoTypeInformation -Append

    }

    else

    {

    $emailid | Select-Object -Property @{n="EmailID";e={$emailid}},@{n="Error";e={"No User exist with this Emailid."}} | Export-Csv ./Errorlogs.csv -NoTypeInformation -Append

    }

}


Sample Output